1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/***************************************************************************
copyright : (C) 2003-2006 by Robby Stephenson
email : robby@periapsis.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "ratingwidget.h"
#include "../field.h"
#include "../latin1literal.h"
#include "../tellico_utils.h"
#include "../tellico_debug.h"
#include <kiconloader.h>
#include <tqintdict.h>
#include <tqlayout.h>
namespace {
static const int RATING_WIDGET_MAX_ICONS = 10; // same as in Field::ratingValues()
static const int RATING_WIDGET_MAX_STAR_SIZE = 24;
}
using Tellico::GUI::RatingWidget;
const TQPixmap& RatingWidget::pixmap(const TQString& value_) {
static TQIntDict<TQPixmap> pixmaps;
if(pixmaps.isEmpty()) {
pixmaps.insert(-1, new TQPixmap());
}
bool ok;
int n = Tellico::toUInt(value_, &ok);
if(!ok || n < 1 || n > 10) {
return *pixmaps[-1];
}
if(pixmaps[n]) {
return *pixmaps[n];
}
TQString picName = TQString::fromLatin1("stars%1").arg(n);
TQPixmap* pix = new TQPixmap(UserIcon(picName));
pixmaps.insert(n, pix);
return *pix;
}
RatingWidget::RatingWidget(Data::FieldPtr field_, TQWidget* parent_, const char* name_/*=0*/)
: TQHBox(parent_, name_), m_field(field_), m_currIndex(-1) {
m_pixOn = UserIcon(TQString::fromLatin1("star_on"));
m_pixOff = UserIcon(TQString::fromLatin1("star_off"));
setSpacing(0);
// find maximum width and height
int w = TQMAX(RATING_WIDGET_MAX_STAR_SIZE, TQMAX(m_pixOn.width(), m_pixOff.width()));
int h = TQMAX(RATING_WIDGET_MAX_STAR_SIZE, TQMAX(m_pixOn.height(), m_pixOff.height()));
for(int i = 0; i < RATING_WIDGET_MAX_ICONS; ++i) {
TQLabel* l = new TQLabel(this);
l->setFixedSize(w, h);
m_widgets.append(l);
}
init();
TQBoxLayout* l = dynamic_cast<TQBoxLayout*>(layout());
if(l) {
l->addStretch(1);
}
}
void RatingWidget::init() {
updateBounds();
m_total = TQMIN(m_max, static_cast<int>(m_widgets.count()));
uint i = 0;
for( ; static_cast<int>(i) < m_total; ++i) {
m_widgets.at(i)->setPixmap(m_pixOff);
}
for( ; i < m_widgets.count(); ++i) {
m_widgets.at(i)->setPixmap(TQPixmap());
}
update();
}
void RatingWidget::updateBounds() {
bool ok; // not used;
m_min = Tellico::toUInt(m_field->property(TQString::fromLatin1("minimum")), &ok);
m_max = Tellico::toUInt(m_field->property(TQString::fromLatin1("maximum")), &ok);
if(m_max > RATING_WIDGET_MAX_ICONS) {
myDebug() << "RatingWidget::updateBounds() - max is too high: " << m_max << endl;
m_max = RATING_WIDGET_MAX_ICONS;
}
if(m_min < 1) {
m_min = 1;
}
}
void RatingWidget::update() {
int i = 0;
for( ; i <= m_currIndex; ++i) {
m_widgets.at(i)->setPixmap(m_pixOn);
}
for( ; i < m_total; ++i) {
m_widgets.at(i)->setPixmap(m_pixOff);
}
TQHBox::update();
}
void RatingWidget::mousePressEvent(TQMouseEvent* event_) {
// only react to left button
if(event_->button() != Qt::LeftButton) {
return;
}
int idx;
TQWidget* child = childAt(event_->pos());
if(child) {
idx = m_widgets.findRef(static_cast<TQLabel*>(child));
// if the widget is clicked beyond the maximum value, clear it
// remember total and min are values, but index is zero-based!
if(idx > m_total-1) {
idx = -1;
} else if(idx < m_min-1) {
idx = m_min-1; // limit to minimum, remember index is zero-based
}
} else {
idx = -1;
}
if(m_currIndex != idx) {
m_currIndex = idx;
update();
emit modified();
}
}
void RatingWidget::clear() {
m_currIndex = -1;
update();
}
TQString RatingWidget::text() const {
// index is index of the list, which is zero-based. Add 1!
return m_currIndex == -1 ? TQString() : TQString::number(m_currIndex+1);
}
void RatingWidget::setText(const TQString& text_) {
bool ok;
// text is value, subtract one to get index
m_currIndex = Tellico::toUInt(text_, &ok)-1;
if(ok) {
if(m_currIndex > m_total-1) {
m_currIndex = -1;
} else if(m_currIndex < m_min-1) {
m_currIndex = m_min-1; // limit to minimum, remember index is zero-based
}
} else {
m_currIndex = -1;
}
update();
}
void RatingWidget::updateField(Data::FieldPtr field_) {
m_field = field_;
init();
}
#include "ratingwidget.moc"
|