33 #include "kcolordialog_p.h"
39 #include <QtGui/QButtonGroup>
40 #include <QtGui/QCheckBox>
41 #include <QtGui/QDesktopWidget>
42 #include <QtGui/QRadioButton>
43 #include <QtGui/qdrawutil.h>
44 #include <QtGui/QActionEvent>
45 #include <QtCore/QFile>
46 #include <QtGui/QHeaderView>
47 #include <QtGui/QImage>
48 #include <QtGui/QStyledItemDelegate>
49 #include <QtGui/QLabel>
50 #include <QtGui/QLayout>
51 #include <QtGui/QPainter>
52 #include <QtGui/QPushButton>
53 #include <QtGui/QScrollBar>
54 #include <QtCore/QTimer>
77 #include "kcolorchoosermode_p.h"
78 #include "kcolorhelpers_p.h"
87 #include <X11/Xutil.h>
92 using namespace KDEPrivate;
94 using KDEPrivate::KColorTable;
96 struct ColorCollectionNameType {
97 const char*
const m_fileName;
98 const char*
const m_displayName;
102 {
"Recent_Colors",
I18N_NOOP2(
"palette name",
"* Recent Colors *") },
103 {
"Custom_Colors",
I18N_NOOP2(
"palette name",
"* Custom Colors *") },
104 {
"40.colors",
I18N_NOOP2(
"palette name",
"Forty Colors") },
105 {
"Oxygen.colors",
I18N_NOOP2(
"palette name",
"Oxygen Colors") },
106 {
"Rainbow.colors",
I18N_NOOP2(
"palette name",
"Rainbow Colors") },
107 {
"Royal.colors",
I18N_NOOP2(
"palette name",
"Royal Colors") },
108 {
"Web.colors",
I18N_NOOP2(
"palette name",
"Web Colors") },
141 virtual void paint(QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index)
const
143 QStyleOptionViewItemV4 opt(option);
144 initStyleOption(&opt,index);
147 QColor backgroundColor = index.data(Qt::BackgroundRole).value<
QColor>();
148 if (backgroundColor.isValid()) {
150 painter->fillRect(opt.rect, backgroundColor);
152 if (opt.state & QStyle::State_Selected) {
156 color =
QColor(255, 255, 255, 220);
160 painter->setRenderHint(QPainter::Antialiasing,
true);
161 painter->setRenderHint(QPainter::HighQualityAntialiasing,
true);
162 painter->setPen(QPen(color, 1.2, Qt::SolidLine));
163 painter->setBrush(QBrush());
164 painter->drawEllipse(opt.rect.adjusted(2,2,-2,-2));
169 backgroundColor = opt.palette.color(QPalette::Window);
170 painter->fillRect(opt.rect, backgroundColor);
172 QColor crossColor = qGray(backgroundColor.rgb()) > 192 ? backgroundColor.darker(106) :
173 backgroundColor.lighter(106);
174 painter->setPen(QPen(crossColor, 1.5));
175 painter->drawLine(opt.rect.topLeft(), opt.rect.bottomRight());
176 painter->drawLine(opt.rect.topRight(), opt.rect.bottomLeft());
185 setItemDelegate(
new KColorCellsItemDelegate(
this));
187 setFrameShape(QFrame::NoFrame);
190 setColumnCount(cols);
192 verticalHeader()->hide();
193 horizontalHeader()->hide();
199 setAcceptDrops(
true);
201 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
202 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
203 viewport()->setBackgroundRole(QPalette::Background);
204 setBackgroundRole(QPalette::Background);
207 setSelectionMode(QAbstractItemView::SingleSelection);
208 setDragEnabled(
false);
218 QTableWidgetItem * tmpItem = item(index / columnCount(), index % columnCount());
221 return tmpItem->data(Qt::BackgroundRole).value<
QColor>();
228 return rowCount() * columnCount();
238 this->setDragEnabled(_acceptDrags);
243 Q_ASSERT(index >= 0 && index <
count());
255 const int tableRow = column / columnCount();
256 const int tableColumn = column % columnCount();
258 Q_ASSERT(tableRow >= 0 && tableRow < rowCount());
259 Q_ASSERT(tableColumn >= 0 && tableColumn < columnCount());
261 QTableWidgetItem * tableItem = item(tableRow, tableColumn);
263 if (tableItem == 0) {
264 tableItem =
new QTableWidgetItem();
265 setItem(tableRow, tableColumn, tableItem);
268 tableItem->setData(Qt::BackgroundRole , color);
310 for (
int index = 0 ; index < columnCount() ; index++)
312 for (
int index = 0 ; index < rowCount() ; index++)
318 return width() / columnCount() ;
323 return height() / rowCount() ;
329 d->mousePos = e->pos();
338 Q_UNUSED(ignoreBorders)
340 QTableWidgetItem* tableItem = itemAt(pos);
345 const int itemRow = row(tableItem);
346 const int itemColumn = column(tableItem);
347 int cell = itemRow * columnCount() + itemColumn;
364 if (this->dragEnabled() || this->acceptDrops()) {
365 if (!(e->buttons() & Qt::LeftButton))
return;
369 if (e->x() > d->mousePos.x() + delay || e->x() < d->mousePos.x() - delay ||
370 e->y() > d->mousePos.y() + delay || e->y() < d->mousePos.y() - delay) {
372 QTableWidgetItem * tableItem = itemAt(d->mousePos);
375 QVariant var = tableItem->data(Qt::BackgroundRole);
377 if (tmpCol.isValid())
388 kDebug() <<
"KColorCells::dragEnterEvent() acceptDrags="
389 << this->dragEnabled()
398 kDebug() <<
"KColorCells::dragMoveEvent() acceptDrags="
399 << this->dragEnabled()
409 kDebug() <<
"KColorCells::dropEvent() color.isValid=" << c.isValid();
411 QTableWidgetItem * tableItem = itemAt(event->pos());
414 tableItem->setData(Qt::BackgroundRole , c);
420 if (selectionMode() != QAbstractItemView::NoSelection) {
426 if (currentCell != cell)
429 if ((cell != -1) && (d->selected != cell)) {
432 const int newRow = cell / columnCount();
433 const int newColumn = cell % columnCount();
437 item(newRow, newColumn)->setSelected(
true);
459 class KColorPatch::KColorPatchPrivate
470 setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
471 setAcceptDrops(
true);
472 setMinimumSize(12, 12);
487 d->color = col.toRgb();
495 QPainter painter(
this);
503 if (!(e->buttons() & Qt::LeftButton))
522 class KColorTable::KColorTablePrivate
525 KColorTablePrivate(KColorTable *q): q(q) {}
527 void slotColorCellSelected(
int index ,
const QColor&);
528 void slotColorCellDoubleClicked(
int index ,
const QColor&);
529 void slotColorTextSelected(
const QString &colorText);
530 void slotSetColors(
const QString &_collectionName);
531 void slotShowNamedColorReadError(
void);
545 KColorTable::KColorTable(
QWidget *parent,
int minWidth,
int cols)
546 :
QWidget(parent), d(new KColorTablePrivate(this))
550 d->mMinWidth = minWidth;
552 d->i18n_namedColors =
i18n(
"Named Colors");
562 paletteList += diskPaletteList;
563 paletteList.append(d->i18n_namedColors);
565 QVBoxLayout *layout =
new QVBoxLayout(
this);
568 d->combo->setEditable(
false);
569 d->combo->addItems(paletteList);
570 layout->addWidget(d->combo);
572 d->sv =
new QScrollArea(
this);
574 d->sv->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
575 d->sv->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
576 QSize minSize =
QSize(d->sv->verticalScrollBar()->sizeHint().width(), 0);
577 minSize +=
QSize(d->sv->frameWidth() * 2, 0);
578 minSize +=
QSize(cellSize);
579 d->sv->setFixedSize(minSize);
580 layout->addWidget(d->sv);
583 d->mNamedColorList->setObjectName(
"namedColorList");
584 d->mNamedColorList->setFixedSize(minSize);
585 d->mNamedColorList->hide();
586 layout->addWidget(d->mNamedColorList);
587 connect(d->mNamedColorList, SIGNAL(currentTextChanged(
QString)),
588 this, SLOT(slotColorTextSelected(
QString)));
590 setFixedSize(sizeHint());
591 connect(d->combo, SIGNAL(activated(
QString)),
592 this, SLOT(slotSetColors(
QString)));
595 KColorTable::~KColorTable()
604 return d->combo->currentText();
625 static const char *
const path[] = {
630 "/usr/share/X11/rgb.txt", 0,
631 "/usr/X11R6/lib/X11/rgb.txt", 0,
632 "/usr/openwin/lib/X11/rgb.txt", 0,
634 "kdeui/rgb.txt",
"data",
645 KColorTable::readNamedColor(
void)
647 if (d->mNamedColorList->count() != 0) {
658 for (
int i = 0; path[i]; i += 2) {
662 if (file.isEmpty()) {
666 file = QString::fromLatin1(path[i]);
669 QFile paletteFile(file);
670 if (!paletteFile.open(QIODevice::ReadOnly)) {
676 while (!paletteFile.atEnd()) {
677 line = paletteFile.readLine();
679 int red, green, blue;
682 if (sscanf(line,
"%d %d %d%n", &red, &green, &blue, &pos) == 3) {
688 QByteArray s1 = line.mid(pos);
689 if (name.isNull() || name.indexOf(
' ') != -1 ||
690 name.indexOf(
"gray") != -1 || name.indexOf(
"grey") != -1) {
694 const QColor color(red, green, blue);
695 if (color.isValid()) {
696 const QString colorName(
i18nc(
"color", name.toLatin1().data()));
697 list.append(colorName);
698 d->m_namedColorMap[ colorName ] = color;
704 d->mNamedColorList->addItems(list);
708 if (d->mNamedColorList->count() == 0) {
716 QTimer::singleShot(10,
this, SLOT(slotShowNamedColorReadError()));
722 KColorTable::KColorTablePrivate::slotShowNamedColorReadError(
void)
724 if (mNamedColorList->count() == 0) {
729 for (
int i = 0; path[i]; i += 2, ++pathCount) {
731 pathMsg += QLatin1String(path[i + 1]) +
", " + QString::fromLatin1(path[i]);
733 pathMsg += QLatin1String(path[i]);
738 QString finalMsg =
i18ncp(
"%1 is the number of paths, %2 is the list of paths (with newlines between them)",
739 "Unable to read X11 RGB color strings. The following "
740 "file location was examined:\n%2",
741 "Unable to read X11 RGB color strings. The following "
742 "file locations were examined:\n%2",
743 pathCount, pathMsg );
761 KColorTable::KColorTablePrivate::slotSetColors(
const QString &_collectionName)
763 q->setColors(_collectionName);
764 if (mNamedColorList->count() && mNamedColorList->isVisible()) {
765 int item = mNamedColorList->currentRow();
766 mNamedColorList->setCurrentRow(item < 0 ? 0 : item);
767 slotColorTextSelected(mNamedColorList->currentItem()->text());
769 slotColorCellSelected(0,
QColor());
775 KColorTable::setColors(
const QString &_collectionName)
777 QString collectionName(_collectionName);
779 if (d->combo->currentText() != collectionName) {
781 for (
int i = 0; i < d->combo->count(); i++) {
782 if (d->combo->itemText(i) == collectionName) {
783 d->combo->setCurrentIndex(i);
789 d->combo->addItem(collectionName);
790 d->combo->setCurrentIndex(d->combo->count() - 1);
812 if (!d->mPalette || d->mPalette->name() != collectionName) {
813 if (collectionName == d->i18n_namedColors) {
815 d->mNamedColorList->show();
818 delete d->cells; d->cells = 0;
819 delete d->mPalette; d->mPalette = 0;
821 d->mNamedColorList->hide();
827 int rows = (d->mPalette->count() + d->mCols - 1) / d->mCols;
828 if (rows < 1) rows = 1;
829 d->cells =
new KColorCells(d->sv->viewport(), rows, d->mCols);
831 d->cells->setAcceptDrags(
false);
832 QSize cellSize =
QSize(d->mMinWidth, d->mMinWidth * rows / d->mCols);
833 d->cells->setFixedSize(cellSize);
834 for (
int i = 0; i < d->mPalette->count(); i++) {
835 d->cells->setColor(i, d->mPalette->color(i));
837 connect(d->cells, SIGNAL(colorSelected(
int,
QColor)),
838 SLOT(slotColorCellSelected(
int,
QColor)));
839 connect(d->cells, SIGNAL(colorDoubleClicked(
int,
QColor)),
840 SLOT(slotColorCellDoubleClicked(
int,
QColor)));
841 d->sv->setWidget(d->cells);
852 KColorTable::KColorTablePrivate::slotColorCellSelected(
int index ,
const QColor& )
854 if (!mPalette || (index >= mPalette->count()))
856 emit q->colorSelected(mPalette->color(index), mPalette->name(index));
860 KColorTable::KColorTablePrivate::slotColorCellDoubleClicked(
int index ,
const QColor& )
862 if (!mPalette || (index >= mPalette->count()))
864 emit q->colorDoubleClicked(mPalette->color(index), mPalette->name(index));
869 KColorTable::KColorTablePrivate::slotColorTextSelected(
const QString &colorText)
871 emit q->colorSelected(m_namedColorMap[ colorText ], colorText);
876 KColorTable::addToCustomColors(
const QColor &color)
879 d->mPalette->addColor(color);
887 KColorTable::addToRecentColors(
const QColor &color)
893 bool recentIsSelected =
false;
897 recentIsSelected =
true;
905 if (recentIsSelected)
909 class KCDPickerFilter;
911 class KColorDialog::KColorDialogPrivate
916 void setRgbEdit(
const QColor &col);
917 void setHsvEdit(
const QColor &col);
918 void setHtmlEdit(
const QColor &col);
922 void slotRGBChanged(
void);
923 void slotAlphaChanged(
void);
924 void slotHSVChanged(
void);
925 void slotHtmlChanged(
void);
926 void slotHSChanged(
int,
int);
927 void slotVChanged(
int);
928 void slotAChanged(
int);
929 void slotModeChanged(
int);
931 void slotColorSelected(
const QColor &col);
932 void slotColorSelected(
const QColor &col,
const QString &name);
933 void slotColorDoubleClicked(
const QColor &col,
const QString &name);
934 void slotColorPicker();
935 void slotAddToCustomColors();
936 void slotDefaultColorClicked();
940 void slotWriteSettings();
976 QButtonGroup *modeGroup;
982 QVBoxLayout* l_right;
983 QGridLayout* tl_layout;
984 QCheckBox *cbDefaultColor;
988 KCDPickerFilter* filter;
993 class KCDPickerFilter:
public QWidget
998 virtual bool x11Event(XEvent* event) {
999 if (event->type == ButtonRelease) {
1000 QMouseEvent e(QEvent::MouseButtonRelease,
QPoint(),
1001 QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
1002 QApplication::sendEvent(parentWidget(), &e);
1004 }
else return false;
1012 :
KDialog(parent), d(new KColorDialogPrivate(this))
1017 d->bRecursion =
true;
1018 d->bColorPicking =
false;
1019 d->bAlphaEnabled =
false;
1023 d->cbDefaultColor = 0L;
1025 connect(
this, SIGNAL(
okClicked()),
this, SLOT(slotWriteSettings()));
1026 connect(
this, SIGNAL(
closeClicked()),
this, SLOT(slotWriteSettings()));
1036 QGridLayout *tl_layout =
new QGridLayout(page);
1037 tl_layout->setMargin(0);
1038 d->tl_layout = tl_layout;
1039 tl_layout->addItem(
new QSpacerItem(
spacingHint()*2, 0), 0, 1);
1045 QVBoxLayout *l_left =
new QVBoxLayout();
1046 tl_layout->addLayout(l_left, 0, 0);
1052 QHBoxLayout *l_ltop =
new QHBoxLayout();
1053 l_left->addLayout(l_ltop);
1059 d->hsSelector->setMinimumSize(256, 256);
1060 l_ltop->addWidget(d->hsSelector, 8);
1061 connect(d->hsSelector, SIGNAL(valueChanged(
int,
int)),
1062 SLOT(slotHSChanged(
int,
int)));
1065 d->valuePal->setMinimumSize(26, 70);
1066 d->valuePal->setIndent(
false);
1067 d->valuePal->setArrowDirection(Qt::RightArrow);
1068 l_ltop->addWidget(d->valuePal, 1);
1069 connect(d->valuePal, SIGNAL(valueChanged(
int)),
1070 SLOT(slotVChanged(
int)));
1073 d->alphaSelector->setFixedSize(256, 26);
1074 d->alphaSelector->setIndent(
false);
1075 d->alphaSelector->setArrowDirection(Qt::DownArrow);
1076 d->alphaSelector->setRange(0, 255);
1077 l_left->addWidget(d->alphaSelector, 1);
1078 connect(d->alphaSelector, SIGNAL(valueChanged(
int)),
1079 SLOT(slotAChanged(
int)));
1082 l_left->addSpacing(10);
1084 QGridLayout *l_lbot =
new QGridLayout();
1085 l_left->addLayout(l_lbot);
1088 QRadioButton *modeButton;
1089 d->modeGroup =
new QButtonGroup(page);
1090 connect(d->modeGroup, SIGNAL(
buttonClicked(
int)), SLOT(slotModeChanged(
int)));
1095 l_lbot->setColumnStretch(2, 10);
1097 modeButton =
new QRadioButton(
i18n(
"Hue:"), page);
1098 l_lbot->addWidget(modeButton, 0, 0);
1099 d->modeGroup->addButton(modeButton,
ChooserHue);
1102 d->hedit->setMaximum(359);
1103 d->hedit->setSuffix(
i18nc(
"The angular degree unit (for hue)",
"\302\260"));
1104 l_lbot->addWidget(d->hedit, 0, 1);
1105 connect(d->hedit, SIGNAL(valueChanged(
int)),
1106 SLOT(slotHSVChanged()));
1108 modeButton =
new QRadioButton(
i18n(
"Saturation:"), page);
1109 l_lbot->addWidget(modeButton, 1, 0);
1113 d->sedit->setMaximum(255);
1114 l_lbot->addWidget(d->sedit, 1, 1);
1115 connect(d->sedit, SIGNAL(valueChanged(
int)),
1116 SLOT(slotHSVChanged()));
1118 modeButton =
new QRadioButton(
i18nc(
"This is the V of HSV",
"Value:"), page);
1119 l_lbot->addWidget(modeButton, 2, 0);
1123 d->vedit->setMaximum(255);
1124 l_lbot->addWidget(d->vedit, 2, 1);
1125 connect(d->vedit, SIGNAL(valueChanged(
int)),
1126 SLOT(slotHSVChanged()));
1132 modeButton =
new QRadioButton(
i18n(
"Red:"), page);
1133 l_lbot->addWidget(modeButton, 0, 3);
1134 d->modeGroup->addButton(modeButton,
ChooserRed);
1137 d->redit->setMaximum(255);
1138 l_lbot->addWidget(d->redit, 0, 4);
1139 connect(d->redit, SIGNAL(valueChanged(
int)),
1140 SLOT(slotRGBChanged()));
1142 modeButton =
new QRadioButton(
i18n(
"Green:"), page);
1143 l_lbot->addWidget(modeButton, 1, 3);
1147 d->gedit->setMaximum(255);
1148 l_lbot->addWidget(d->gedit, 1, 4);
1149 connect(d->gedit, SIGNAL(valueChanged(
int)),
1150 SLOT(slotRGBChanged()));
1152 modeButton =
new QRadioButton(
i18n(
"Blue:"), page);
1153 l_lbot->addWidget(modeButton, 2, 3);
1157 d->bedit->setMaximum(255);
1158 l_lbot->addWidget(d->bedit, 2, 4);
1159 connect(d->bedit, SIGNAL(valueChanged(
int)),
1160 SLOT(slotRGBChanged()));
1162 d->alphaLabel =
new KHBox(page);
1164 label =
new QLabel(
i18n(
"Alpha:"), d->alphaLabel);
1165 QStyleOptionButton option;
1166 option.initFrom(modeButton);
1167 QRect labelRect = modeButton->style()->subElementRect(QStyle::SE_RadioButtonContents, &option, modeButton);
1168 int indent = layoutDirection() == Qt::LeftToRight ? labelRect.left() : modeButton->geometry().right() - labelRect.right();
1169 spacer->setFixedWidth(indent);
1170 l_lbot->addWidget(d->alphaLabel, 3, 3);
1173 d->aedit->setMaximum(255);
1174 label->setBuddy(d->aedit);
1175 l_lbot->addWidget(d->aedit, 3, 4);
1176 connect(d->aedit, SIGNAL(valueChanged(
int)),
1177 SLOT(slotAlphaChanged()));
1179 d->aedit->setVisible(
false);
1180 d->alphaLabel->setVisible(
false);
1181 d->alphaSelector->setVisible(
false);
1186 d->l_right =
new QVBoxLayout;
1187 tl_layout->addLayout(d->l_right, 0, 2);
1192 d->table =
new KColorTable(page);
1193 d->l_right->addWidget(d->table, 10);
1204 d->originalPalette = d->table->name();
1209 d->l_right->addSpacing(10);
1211 QHBoxLayout *l_hbox =
new QHBoxLayout();
1212 d->l_right->addItem(l_hbox);
1218 addButton->setText(
i18n(
"&Add to Custom Colors"));
1219 l_hbox->addWidget(addButton, 0, Qt::AlignLeft);
1220 connect(addButton, SIGNAL(clicked()), SLOT(slotAddToCustomColors()));
1226 button->setIcon(
KIcon(
"color-picker"));
1227 int commonHeight = addButton->sizeHint().height();
1228 button->setFixedSize(commonHeight, commonHeight);
1229 l_hbox->addWidget(button, 0, Qt::AlignHCenter);
1230 connect(button, SIGNAL(clicked()), SLOT(slotColorPicker()));
1235 d->l_right->addSpacing(10);
1240 QGridLayout *l_grid =
new QGridLayout();
1241 d->l_right->addLayout(l_grid);
1243 l_grid->setColumnStretch(2, 1);
1245 label =
new QLabel(page);
1246 label->setText(
i18n(
"Name:"));
1247 l_grid->addWidget(label, 0, 1, Qt::AlignLeft);
1249 d->colorName =
new QLabel(page);
1250 l_grid->addWidget(d->colorName, 0, 2, Qt::AlignLeft);
1252 label =
new QLabel(page);
1253 label->setText(
i18n(
"HTML:"));
1254 l_grid->addWidget(label, 1, 1, Qt::AlignLeft);
1257 d->htmlName->setMaxLength(13);
1258 d->htmlName->setText(
"#FFFFFF");
1259 int w = d->htmlName->fontMetrics().width(QLatin1String(
"#DDDDDDD"));
1260 d->htmlName->setFixedWidth(w);
1261 l_grid->addWidget(d->htmlName, 1, 2, Qt::AlignLeft);
1263 connect(d->htmlName, SIGNAL(textChanged(
QString)),
1264 SLOT(slotHtmlChanged()));
1267 d->patch->setFixedSize(48, 48);
1268 l_grid->addWidget(d->patch, 0, 0, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
1269 connect(d->patch, SIGNAL(colorChanged(
QColor)),
1275 setTabOrder(d->hedit, d->sedit);
1276 setTabOrder(d->sedit, d->vedit);
1277 setTabOrder(d->vedit, d->redit);
1278 setTabOrder(d->redit, d->gedit);
1279 setTabOrder(d->gedit, d->bedit);
1280 setTabOrder(d->bedit, d->aedit);
1282 tl_layout->activate();
1283 page->setMinimumSize(page->sizeHint());
1286 d->bRecursion =
false;
1287 d->bEditHsv =
false;
1288 d->bEditRgb =
false;
1289 d->bEditHtml =
false;
1293 col.setHsv(0, 0, 255);
1300 d->hsSelector->setAcceptDrops(
true);
1308 if (d->bColorPicking &&
kapp)
1309 kapp->removeX11EventFilter(d->filter);
1317 if ((obj == d->htmlName) || (obj == d->hsSelector))
1318 switch (ev->type()) {
1319 case QEvent::DragEnter:
1320 case QEvent::DragMove:
1321 case QEvent::DragLeave:
1323 case QEvent::DragResponse:
1324 qApp->sendEvent(d->patch, ev);
1335 if (!d->cbDefaultColor) {
1339 d->l_right->addSpacing(10);
1344 d->cbDefaultColor =
new QCheckBox(
i18n(
"Default color"),
mainWidget());
1346 d->l_right->addWidget(d->cbDefaultColor);
1348 mainWidget()->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
1349 d->tl_layout->activate();
1353 connect(d->cbDefaultColor, SIGNAL(clicked()), SLOT(slotDefaultColorClicked()));
1356 d->defaultColor = col;
1358 d->slotDefaultColorClicked();
1363 return d->defaultColor;
1368 if (d->bAlphaEnabled != alpha) {
1369 d->bAlphaEnabled = alpha;
1370 d->aedit->setVisible(d->bAlphaEnabled);
1371 d->alphaLabel->setVisible(d->bAlphaEnabled);
1372 d->alphaSelector->setVisible(d->bAlphaEnabled);
1374 mainWidget()->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
1375 d->tl_layout->activate();
1383 return d->bAlphaEnabled;
1390 hsSelector->setChooserMode(c);
1391 valuePal->setChooserMode(c);
1393 modeGroup->button(valuePal->chooserMode())->setChecked(
true);
1394 valuePal->updateContents();
1395 hsSelector->updateContents();
1397 hsSelector->update();
1407 void KColorDialog::KColorDialogPrivate::slotDefaultColorClicked()
1409 if (cbDefaultColor->isChecked()) {
1410 selColor = defaultColor;
1411 showColor(selColor,
i18n(
"-default-"));
1413 showColor(selColor,
QString());
1415 emit q->colorSelected(selColor);
1419 KColorDialog::KColorDialogPrivate::slotModeChanged(
int id)
1425 KColorDialog::readSettings()
1429 QString collectionName =
group.readEntry(
"CurrentPalette");
1430 if (collectionName.isEmpty()) {
1441 d->table->setColors(collectionName);
1445 KColorDialog::KColorDialogPrivate::slotWriteSettings()
1449 QString collectionName = table->name();
1450 if (!
group.hasDefault(
"CurrentPalette") && table->name() == originalPalette) {
1451 group.revertToDefault(
"CurrentPalette");
1453 QString collectionName(table->name());
1460 group.writeEntry(
"CurrentPalette", collectionName);
1467 if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
1469 if (d->selColor.isValid())
1470 d->table->addToRecentColors(d->selColor);
1485 dlg.setObjectName(
"Color Selector");
1486 if (theColor.isValid())
1488 int result = dlg.exec();
1490 if (result == Accepted) {
1491 theColor = dlg.
color();
1503 dlg.setObjectName(
"Color Selector");
1506 int result = dlg.exec();
1508 if (result == Accepted)
1509 theColor = dlg.
color();
1514 void KColorDialog::KColorDialogPrivate::slotRGBChanged(
void)
1516 if (bRecursion)
return;
1517 int red = redit->value();
1518 int grn = gedit->value();
1519 int blu = bedit->value();
1521 if (red > 255 || red < 0)
return;
1522 if (grn > 255 || grn < 0)
return;
1523 if (blu > 255 || blu < 0)
return;
1526 col.setRgb(red, grn, blu, aedit->value());
1532 void KColorDialog::KColorDialogPrivate::slotAlphaChanged(
void)
1534 if (bRecursion)
return;
1535 int alpha = aedit->value();
1537 if (alpha > 255 || alpha < 0)
return;
1540 col.setAlpha(alpha);
1544 void KColorDialog::KColorDialogPrivate::slotHtmlChanged(
void)
1546 if (bRecursion || htmlName->text().isEmpty())
return;
1548 QString strColor(htmlName->text());
1551 if (strColor[0] !=
'#') {
1552 bool signalsblocked = htmlName->blockSignals(
true);
1553 strColor.prepend(
"#");
1554 htmlName->setText(strColor);
1555 htmlName->blockSignals(signalsblocked);
1558 const QColor color(strColor);
1560 if (color.isValid()) {
1568 void KColorDialog::KColorDialogPrivate::slotHSVChanged(
void)
1570 if (bRecursion)
return;
1571 int hue = hedit->value();
1572 int sat = sedit->value();
1573 int val = vedit->value();
1575 if (hue > 359 || hue < 0)
return;
1576 if (sat > 255 || sat < 0)
return;
1577 if (val > 255 || val < 0)
return;
1580 col.setHsv(hue, sat, val, aedit->value());
1586 void KColorDialog::KColorDialogPrivate::slotHSChanged(
int x,
int y)
1596 void KColorDialog::KColorDialogPrivate::slotVChanged(
int v)
1603 void KColorDialog::KColorDialogPrivate::slotAChanged(
int value)
1606 col.setAlpha(value);
1610 void KColorDialog::KColorDialogPrivate::slotColorSelected(
const QColor &color)
1615 void KColorDialog::KColorDialogPrivate::slotAddToCustomColors()
1617 table->addToCustomColors(selColor);
1620 void KColorDialog::KColorDialogPrivate::slotColorSelected(
const QColor &color,
const QString &name)
1622 _setColor(color, name);
1625 void KColorDialog::KColorDialogPrivate::slotColorDoubleClicked
1631 _setColor(color, name);
1635 void KColorDialog::KColorDialogPrivate::_setColor(
const QColor &color,
const QString &name)
1637 if (color.isValid()) {
1638 if (cbDefaultColor && cbDefaultColor->isChecked())
1639 cbDefaultColor->setChecked(
false);
1642 if (cbDefaultColor && cbDefaultColor->isChecked())
1643 cbDefaultColor->setChecked(
true);
1644 selColor = defaultColor;
1647 showColor(selColor, name);
1649 emit q->colorSelected(selColor);
1653 void KColorDialog::KColorDialogPrivate::showColor(
const QColor &color,
const QString &name)
1658 colorName->setText(
i18n(
"-unnamed-"));
1660 colorName->setText(name);
1662 patch->setColor(color);
1667 aedit->setValue(color.alpha());
1669 QColor rgbColor = color.toRgb();
1670 bool ltr = q->layoutDirection() == Qt::LeftToRight;
1671 rgbColor.setAlpha(ltr ? 0 : 255);
1672 alphaSelector->setFirstColor(rgbColor);
1673 rgbColor.setAlpha(ltr ? 255 : 0);
1674 alphaSelector->setSecondColor(rgbColor);
1675 alphaSelector->setValue(color.alpha());
1682 hsSelector->setValues(xValue, yValue);
1683 valuePal->setValue(value);
1685 bool blocked = valuePal->blockSignals(
true);
1687 valuePal->setHue(color.hue());
1688 valuePal->setSaturation(color.saturation());
1689 valuePal->setColorValue(color.value());
1690 valuePal->updateContents();
1691 valuePal->blockSignals(blocked);
1694 blocked = hsSelector->blockSignals(
true);
1696 hsSelector->setHue(color.hue());
1697 hsSelector->setSaturation(color.saturation());
1698 hsSelector->setColorValue(color.value());
1699 hsSelector->updateContents();
1700 hsSelector->blockSignals(blocked);
1701 hsSelector->update();
1709 KColorDialog::KColorDialogPrivate::slotColorPicker()
1711 bColorPicking =
true;
1713 filter =
new KCDPickerFilter(q);
1714 kapp->installX11EventFilter(filter);
1716 q->grabMouse(Qt::CrossCursor);
1723 if (d->bColorPicking) {
1724 d->_setColor(
grabColor(e->globalPos()));
1734 if (d->bColorPicking) {
1735 d->bColorPicking =
false;
1737 kapp->removeX11EventFilter(d->filter);
1738 delete d->filter; d->filter = 0;
1742 d->_setColor(
grabColor(e->globalPos()));
1755 if( !qApp->desktop()->geometry().contains( p ))
1757 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
1758 XImage *ximg = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
1759 unsigned long xpixel = XGetPixel(ximg, 0, 0);
1760 XDestroyImage(ximg);
1762 xcol.pixel = xpixel;
1763 xcol.flags = DoRed | DoGreen | DoBlue;
1764 XQueryColor(QX11Info::display(),
1765 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
1767 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
1769 QWidget *desktop = QApplication::desktop();
1770 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
1771 QImage i = pm.toImage();
1772 return i.pixel(0, 0);
1779 if (d->bColorPicking) {
1780 if (e->key() == Qt::Key_Escape) {
1781 d->bColorPicking =
false;
1783 kapp->removeX11EventFilter(d->filter);
1784 delete d->filter; d->filter = 0;
1795 void KColorDialog::KColorDialogPrivate::setRgbEdit(
const QColor &col)
1797 if (bEditRgb)
return;
1799 col.getRgb(&r, &g, &b);
1806 void KColorDialog::KColorDialogPrivate::setHtmlEdit(
const QColor &col)
1808 if (bEditHtml)
return;
1810 col.getRgb(&r, &g, &b);
1813 num.sprintf(
"#%02X%02X%02X", r, g, b);
1814 htmlName->setText(num);
1818 void KColorDialog::KColorDialogPrivate::setHsvEdit(
const QColor &col)
1820 if (bEditHsv)
return;
1822 col.getHsv(&h, &s, &v);
1829 #include "kcolordialog.moc"
1830 #include "kcolordialog_p.moc"