kalarm/lib
colourcombo.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qpainter.h>
00025
00026 #include <klocale.h>
00027 #include <kcolordialog.h>
00028
00029 #include "kalarmapp.h"
00030 #include "preferences.h"
00031 #include "colourcombo.moc"
00032
00033
00034 ColourCombo::ColourCombo(QWidget* parent, const char* name, const QColor& defaultColour)
00035 : QComboBox(parent, name),
00036 mColourList(Preferences::messageColours()),
00037 mSelectedColour(defaultColour),
00038 mCustomColour(255, 255, 255),
00039 mReadOnly(false),
00040 mDisabled(false)
00041 {
00042 addColours();
00043 connect(this, SIGNAL(activated(int)), SLOT(slotActivated(int)));
00044 connect(this, SIGNAL(highlighted(int)), SLOT(slotHighlighted(int)));
00045 Preferences::connect(SIGNAL(preferencesChanged()), this, SLOT(slotPreferencesChanged()));
00046 }
00047
00048 void ColourCombo::setColour(const QColor& colour)
00049 {
00050 mSelectedColour = colour;
00051 addColours();
00052 }
00053
00054
00055
00056
00057 void ColourCombo::setColours(const ColourList& colours)
00058 {
00059 mColourList = colours;
00060 if (mSelectedColour != mCustomColour
00061 && !mColourList.contains(mSelectedColour))
00062 {
00063
00064 mSelectedColour = mColourList.count() ? mColourList.first() : mCustomColour;
00065 }
00066 addColours();
00067 }
00068
00069
00070
00071
00072
00073 void ColourCombo::slotPreferencesChanged()
00074 {
00075 const ColourList& prefColours = Preferences::messageColours();
00076 if (prefColours != mColourList)
00077 setColours(prefColours);
00078 }
00079
00080
00081
00082
00083
00084 void ColourCombo::setEnabled(bool enable)
00085 {
00086 if (enable && mDisabled)
00087 {
00088 mDisabled = false;
00089 setColour(mSelectedColour);
00090 }
00091 else if (!enable && !mDisabled)
00092 {
00093 mSelectedColour = color();
00094 int end = count();
00095 if (end > 1)
00096 {
00097
00098 QPixmap pm = *pixmap(1);
00099 pm.fill(paletteBackgroundColor());
00100 insertItem(pm);
00101 setCurrentItem(end);
00102 }
00103 mDisabled = true;
00104 }
00105 QComboBox::setEnabled(enable);
00106 }
00107
00108 void ColourCombo::slotActivated(int index)
00109 {
00110 if (index)
00111 mSelectedColour = mColourList[index - 1];
00112 else
00113 {
00114 if (KColorDialog::getColor(mCustomColour, this) == QDialog::Accepted)
00115 {
00116 QRect rect;
00117 drawCustomItem(rect, false);
00118 }
00119 mSelectedColour = mCustomColour;
00120 }
00121 emit activated(mSelectedColour);
00122 }
00123
00124 void ColourCombo::slotHighlighted(int index)
00125 {
00126 mSelectedColour = index ? mColourList[index - 1] : mCustomColour;
00127 emit highlighted(mSelectedColour);
00128 }
00129
00130
00131
00132
00133 void ColourCombo::addColours()
00134 {
00135 clear();
00136
00137 for (ColourList::const_iterator it = mColourList.begin(); ; ++it)
00138 {
00139 if (it == mColourList.end())
00140 {
00141 mCustomColour = mSelectedColour;
00142 break;
00143 }
00144 if (mSelectedColour == *it)
00145 break;
00146 }
00147
00148 QRect rect;
00149 drawCustomItem(rect, true);
00150
00151 QPainter painter;
00152 QPixmap pixmap(rect.width(), rect.height());
00153 int i = 1;
00154 for (ColourList::const_iterator it = mColourList.begin(); it != mColourList.end(); ++i, ++it)
00155 {
00156 painter.begin(&pixmap);
00157 QBrush brush(*it);
00158 painter.fillRect(rect, brush);
00159 painter.end();
00160
00161 insertItem(pixmap);
00162 pixmap.detach();
00163
00164 if (*it == mSelectedColour.rgb())
00165 setCurrentItem(i);
00166 }
00167 }
00168
00169 void ColourCombo::drawCustomItem(QRect& rect, bool insert)
00170 {
00171 QPen pen;
00172 if (qGray(mCustomColour.rgb()) < 128)
00173 pen.setColor(Qt::white);
00174 else
00175 pen.setColor(Qt::black);
00176
00177 QPainter painter;
00178 QFontMetrics fm = QFontMetrics(painter.font());
00179 rect.setRect(0, 0, width(), fm.height() + 4);
00180 QPixmap pixmap(rect.width(), rect.height());
00181
00182 painter.begin(&pixmap);
00183 QBrush brush(mCustomColour);
00184 painter.fillRect(rect, brush);
00185 painter.setPen(pen);
00186 painter.drawText(2, fm.ascent() + 2, i18n("Custom..."));
00187 painter.end();
00188
00189 if (insert)
00190 insertItem(pixmap);
00191 else
00192 changeItem(pixmap, 0);
00193 pixmap.detach();
00194 }
00195
00196 void ColourCombo::setReadOnly(bool ro)
00197 {
00198 mReadOnly = ro;
00199 }
00200
00201 void ColourCombo::resizeEvent(QResizeEvent* re)
00202 {
00203 QComboBox::resizeEvent(re);
00204 addColours();
00205 }
00206
00207 void ColourCombo::mousePressEvent(QMouseEvent* e)
00208 {
00209 if (mReadOnly)
00210 {
00211
00212 if (e->button() == LeftButton)
00213 return;
00214 }
00215 QComboBox::mousePressEvent(e);
00216 }
00217
00218 void ColourCombo::mouseReleaseEvent(QMouseEvent* e)
00219 {
00220 if (!mReadOnly)
00221 QComboBox::mouseReleaseEvent(e);
00222 }
00223
00224 void ColourCombo::mouseMoveEvent(QMouseEvent* e)
00225 {
00226 if (!mReadOnly)
00227 QComboBox::mouseMoveEvent(e);
00228 }
00229
00230 void ColourCombo::keyPressEvent(QKeyEvent* e)
00231 {
00232 if (!mReadOnly || e->key() == Qt::Key_Escape)
00233 QComboBox::keyPressEvent(e);
00234 }
00235
00236 void ColourCombo::keyReleaseEvent(QKeyEvent* e)
00237 {
00238 if (!mReadOnly)
00239 QComboBox::keyReleaseEvent(e);
00240 }
|