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"
94using KDEPrivate::KColorTable;
96struct 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") },
121class KColorCells::KColorCellsPrivate
137class KColorCellsItemDelegate:
public QStyledItemDelegate
140 KColorCellsItemDelegate(
KColorCells *parent): QStyledItemDelegate(parent) {}
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) {
154 QColor color = QColor(0, 0, 0, 220);
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();
243 this->setDragEnabled(_acceptDrags);
248 return this->dragEnabled();
253 Q_ASSERT(index >= 0 && index <
count());
265 const int tableRow = column / columnCount();
266 const int tableColumn = column % columnCount();
268 Q_ASSERT(tableRow >= 0 && tableRow < rowCount());
269 Q_ASSERT(tableColumn >= 0 && tableColumn < columnCount());
271 QTableWidgetItem * tableItem = item(tableRow, tableColumn);
273 if (tableItem == 0) {
274 tableItem =
new QTableWidgetItem();
275 setItem(tableRow, tableColumn, tableItem);
278 tableItem->setData(Qt::BackgroundRole , color);
320 for (
int index = 0 ; index < columnCount() ; index++)
322 for (
int index = 0 ; index < rowCount() ; index++)
328 return width() / columnCount() ;
333 return height() / rowCount() ;
339 d->mousePos = e->pos();
341 QTableWidget::mousePressEvent(e);
348 Q_UNUSED(ignoreBorders)
350 QTableWidgetItem* tableItem = itemAt(pos);
355 const int itemRow = row(tableItem);
356 const int itemColumn = column(tableItem);
357 int cell = itemRow * columnCount() + itemColumn;
374 if (this->dragEnabled() || this->acceptDrops()) {
375 if (!(e->buttons() & Qt::LeftButton))
return;
379 if (e->x() > d->mousePos.x() + delay || e->x() < d->mousePos.x() - delay ||
380 e->y() > d->mousePos.y() + delay || e->y() < d->mousePos.y() - delay) {
382 QTableWidgetItem * tableItem = itemAt(d->mousePos);
385 QVariant var = tableItem->data(Qt::BackgroundRole);
386 QColor tmpCol = var.value<QColor>();
387 if (tmpCol.isValid())
393 QTableWidget::mouseMoveEvent(e);
398 kDebug() <<
"KColorCells::dragEnterEvent() acceptDrags="
399 << this->dragEnabled()
408 kDebug() <<
"KColorCells::dragMoveEvent() acceptDrags="
409 << this->dragEnabled()
419 kDebug() <<
"KColorCells::dropEvent() color.isValid=" << c.isValid();
421 QTableWidgetItem * tableItem = itemAt(event->pos());
424 tableItem->setData(Qt::BackgroundRole , c);
430 if (selectionMode() != QAbstractItemView::NoSelection) {
436 if (currentCell != cell)
439 if ((cell != -1) && (d->selected != cell)) {
442 const int newRow = cell / columnCount();
443 const int newColumn = cell % columnCount();
447 item(newRow, newColumn)->setSelected(
true);
455 QTableWidget::mouseReleaseEvent(e);
469class KColorPatch::KColorPatchPrivate
480 setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
481 setAcceptDrops(
true);
482 setMinimumSize(12, 12);
497 d->color = col.toRgb();
504 QFrame::paintEvent(pe);
505 QPainter painter(
this);
513 if (!(e->buttons() & Qt::LeftButton))
532class KColorTable::KColorTablePrivate
535 KColorTablePrivate(KColorTable *q): q(q) {}
537 void slotColorCellSelected(
int index ,
const QColor&);
538 void slotColorCellDoubleClicked(
int index ,
const QColor&);
539 void slotColorTextSelected(
const QString &colorText);
540 void slotSetColors(
const QString &_collectionName);
541 void slotShowNamedColorReadError(
void);
544 QString i18n_namedColors;
555KColorTable::KColorTable(
QWidget *parent,
int minWidth,
int cols)
556 :
QWidget(parent), d(new KColorTablePrivate(this))
560 d->mMinWidth = minWidth;
562 d->i18n_namedColors =
i18n(
"Named Colors");
565 QStringList paletteList;
572 paletteList += diskPaletteList;
573 paletteList.append(d->i18n_namedColors);
575 QVBoxLayout *layout =
new QVBoxLayout(
this);
578 d->combo->setEditable(
false);
579 d->combo->addItems(paletteList);
580 layout->addWidget(d->combo);
582 d->sv =
new QScrollArea(
this);
583 QSize cellSize = QSize(d->mMinWidth, 120);
584 d->sv->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
585 d->sv->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
586 QSize minSize = QSize(d->sv->verticalScrollBar()->sizeHint().width(), 0);
587 minSize += QSize(d->sv->frameWidth() * 2, 0);
588 minSize += QSize(cellSize);
589 d->sv->setFixedSize(minSize);
590 layout->addWidget(d->sv);
593 d->mNamedColorList->setObjectName(
"namedColorList");
594 d->mNamedColorList->setFixedSize(minSize);
595 d->mNamedColorList->hide();
596 layout->addWidget(d->mNamedColorList);
597 connect(d->mNamedColorList, SIGNAL(currentTextChanged(QString)),
598 this, SLOT(slotColorTextSelected(QString)));
600 setFixedSize(sizeHint());
601 connect(d->combo, SIGNAL(activated(QString)),
602 this, SLOT(slotSetColors(QString)));
605KColorTable::~KColorTable()
612KColorTable::name()
const
614 return d->combo->currentText();
635 static const char *
const path[] = {
640 "/usr/share/X11/rgb.txt", 0,
641 "/usr/X11R6/lib/X11/rgb.txt", 0,
642 "/usr/openwin/lib/X11/rgb.txt", 0,
644 "kdeui/rgb.txt",
"data",
655KColorTable::readNamedColor(
void)
657 if (d->mNamedColorList->count() != 0) {
668 for (
int i = 0; path[i]; i += 2) {
672 if (file.isEmpty()) {
676 file = QString::fromLatin1(path[i]);
679 QFile paletteFile(file);
680 if (!paletteFile.open(QIODevice::ReadOnly)) {
686 while (!paletteFile.atEnd()) {
687 line = paletteFile.readLine();
689 int red, green, blue;
692 if (sscanf(line,
"%d %d %d%n", &red, &green, &blue, &pos) == 3) {
697 QString
name = line.mid(pos).trimmed();
698 QByteArray s1 = line.mid(pos);
699 if (
name.isNull() ||
name.indexOf(
' ') != -1 ||
700 name.indexOf(
"gray") != -1 ||
name.indexOf(
"grey") != -1) {
704 const QColor color(red, green, blue);
705 if (color.isValid()) {
706 const QString colorName(
i18nc(
"color",
name.toLatin1().data()));
707 list.append(colorName);
708 d->m_namedColorMap[ colorName ] = color;
714 d->mNamedColorList->addItems(list);
718 if (d->mNamedColorList->count() == 0) {
726 QTimer::singleShot(10,
this, SLOT(slotShowNamedColorReadError()));
732KColorTable::KColorTablePrivate::slotShowNamedColorReadError(
void)
734 if (mNamedColorList->count() == 0) {
739 for (
int i = 0; path[i]; i += 2, ++pathCount) {
741 pathMsg += QLatin1String(path[i + 1]) +
", " + QString::fromLatin1(path[i]);
743 pathMsg += QLatin1String(path[i]);
748 QString finalMsg =
i18ncp(
"%1 is the number of paths, %2 is the list of paths (with newlines between them)",
749 "Unable to read X11 RGB color strings. The following "
750 "file location was examined:\n%2",
751 "Unable to read X11 RGB color strings. The following "
752 "file locations were examined:\n%2",
753 pathCount, pathMsg );
771KColorTable::KColorTablePrivate::slotSetColors(
const QString &_collectionName)
773 q->setColors(_collectionName);
774 if (mNamedColorList->count() && mNamedColorList->isVisible()) {
775 int item = mNamedColorList->currentRow();
776 mNamedColorList->setCurrentRow(item < 0 ? 0 : item);
777 slotColorTextSelected(mNamedColorList->currentItem()->text());
779 slotColorCellSelected(0, QColor());
785KColorTable::setColors(
const QString &_collectionName)
787 QString collectionName(_collectionName);
789 if (d->combo->currentText() != collectionName) {
791 for (
int i = 0; i < d->combo->count(); i++) {
792 if (d->combo->itemText(i) == collectionName) {
793 d->combo->setCurrentIndex(i);
799 d->combo->addItem(collectionName);
800 d->combo->setCurrentIndex(d->combo->count() - 1);
822 if (!d->mPalette || d->mPalette->name() != collectionName) {
823 if (collectionName == d->i18n_namedColors) {
825 d->mNamedColorList->show();
828 delete d->cells; d->cells = 0;
829 delete d->mPalette; d->mPalette = 0;
831 d->mNamedColorList->hide();
837 int rows = (d->mPalette->count() + d->mCols - 1) / d->mCols;
838 if (rows < 1) rows = 1;
839 d->cells =
new KColorCells(d->sv->viewport(), rows, d->mCols);
840 d->cells->setShading(
false);
841 d->cells->setAcceptDrags(
false);
842 QSize cellSize = QSize(d->mMinWidth, d->mMinWidth * rows / d->mCols);
843 d->cells->setFixedSize(cellSize);
844 for (
int i = 0; i < d->mPalette->count(); i++) {
845 d->cells->setColor(i, d->mPalette->color(i));
847 connect(d->cells, SIGNAL(colorSelected(
int,QColor)),
848 SLOT(slotColorCellSelected(
int,QColor)));
849 connect(d->cells, SIGNAL(colorDoubleClicked(
int,QColor)),
850 SLOT(slotColorCellDoubleClicked(
int,QColor)));
851 d->sv->setWidget(d->cells);
862KColorTable::KColorTablePrivate::slotColorCellSelected(
int index ,
const QColor& )
864 if (!mPalette || (index >= mPalette->count()))
866 emit q->colorSelected(mPalette->color(index), mPalette->name(index));
870KColorTable::KColorTablePrivate::slotColorCellDoubleClicked(
int index ,
const QColor& )
872 if (!mPalette || (index >= mPalette->count()))
874 emit q->colorDoubleClicked(mPalette->color(index), mPalette->name(index));
879KColorTable::KColorTablePrivate::slotColorTextSelected(
const QString &colorText)
881 emit q->colorSelected(m_namedColorMap[ colorText ], colorText);
886KColorTable::addToCustomColors(
const QColor &color)
889 d->mPalette->addColor(color);
897KColorTable::addToRecentColors(
const QColor &color)
903 bool recentIsSelected =
false;
907 recentIsSelected =
true;
915 if (recentIsSelected)
919class KCDPickerFilter;
921class KColorDialog::KColorDialogPrivate
926 void setRgbEdit(
const QColor &col);
927 void setHsvEdit(
const QColor &col);
928 void setHtmlEdit(
const QColor &col);
929 void _setColor(
const QColor &col,
const QString &name = QString());
930 void showColor(
const QColor &
color,
const QString &name);
932 void slotRGBChanged(
void);
933 void slotAlphaChanged(
void);
934 void slotHSVChanged(
void);
935 void slotHtmlChanged(
void);
936 void slotHSChanged(
int,
int);
937 void slotVChanged(
int);
938 void slotAChanged(
int);
939 void slotModeChanged(
int);
941 void slotColorSelected(
const QColor &col);
942 void slotColorSelected(
const QColor &col,
const QString &name);
943 void slotColorDoubleClicked(
const QColor &col,
const QString &name);
944 void slotColorPicker();
945 void slotAddToCustomColors();
946 void slotDefaultColorClicked();
950 void slotWriteSettings();
964 QString originalPalette;
986 QButtonGroup *modeGroup;
992 QVBoxLayout* l_right;
993 QGridLayout* tl_layout;
994 QCheckBox *cbDefaultColor;
998 KCDPickerFilter* filter;
1003class KCDPickerFilter:
public QWidget
1008 virtual bool x11Event(XEvent* event) {
1009 if (event->type == ButtonRelease) {
1010 QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
1011 QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
1012 QApplication::sendEvent(parentWidget(), &e);
1014 }
else return false;
1022 :
KDialog(parent), d(new KColorDialogPrivate(this))
1027 d->bRecursion =
true;
1028 d->bColorPicking =
false;
1029 d->bAlphaEnabled =
false;
1033 d->cbDefaultColor = 0L;
1035 connect(
this, SIGNAL(
okClicked()),
this, SLOT(slotWriteSettings()));
1036 connect(
this, SIGNAL(
closeClicked()),
this, SLOT(slotWriteSettings()));
1046 QGridLayout *tl_layout =
new QGridLayout(page);
1047 tl_layout->setMargin(0);
1048 d->tl_layout = tl_layout;
1049 tl_layout->addItem(
new QSpacerItem(
spacingHint()*2, 0), 0, 1);
1055 QVBoxLayout *l_left =
new QVBoxLayout();
1056 tl_layout->addLayout(l_left, 0, 0);
1062 QHBoxLayout *l_ltop =
new QHBoxLayout();
1063 l_left->addLayout(l_ltop);
1069 d->hsSelector->setMinimumSize(256, 256);
1070 l_ltop->addWidget(d->hsSelector, 8);
1071 connect(d->hsSelector, SIGNAL(valueChanged(
int,
int)),
1072 SLOT(slotHSChanged(
int,
int)));
1075 d->valuePal->setMinimumSize(26, 70);
1076 d->valuePal->setIndent(
false);
1077 d->valuePal->setArrowDirection(Qt::RightArrow);
1078 l_ltop->addWidget(d->valuePal, 1);
1079 connect(d->valuePal, SIGNAL(valueChanged(
int)),
1080 SLOT(slotVChanged(
int)));
1083 d->alphaSelector->setFixedSize(256, 26);
1084 d->alphaSelector->setIndent(
false);
1085 d->alphaSelector->setArrowDirection(Qt::DownArrow);
1086 d->alphaSelector->setRange(0, 255);
1087 l_left->addWidget(d->alphaSelector, 1);
1088 connect(d->alphaSelector, SIGNAL(valueChanged(
int)),
1089 SLOT(slotAChanged(
int)));
1092 l_left->addSpacing(10);
1094 QGridLayout *l_lbot =
new QGridLayout();
1095 l_left->addLayout(l_lbot);
1098 QRadioButton *modeButton;
1099 d->modeGroup =
new QButtonGroup(page);
1100 connect(d->modeGroup, SIGNAL(
buttonClicked(
int)), SLOT(slotModeChanged(
int)));
1105 l_lbot->setColumnStretch(2, 10);
1107 modeButton =
new QRadioButton(
i18n(
"Hue:"), page);
1108 l_lbot->addWidget(modeButton, 0, 0);
1109 d->modeGroup->addButton(modeButton,
ChooserHue);
1112 d->hedit->setMaximum(359);
1113 d->hedit->setSuffix(
i18nc(
"The angular degree unit (for hue)",
"\302\260"));
1114 l_lbot->addWidget(d->hedit, 0, 1);
1115 connect(d->hedit, SIGNAL(valueChanged(
int)),
1116 SLOT(slotHSVChanged()));
1118 modeButton =
new QRadioButton(
i18n(
"Saturation:"), page);
1119 l_lbot->addWidget(modeButton, 1, 0);
1123 d->sedit->setMaximum(255);
1124 l_lbot->addWidget(d->sedit, 1, 1);
1125 connect(d->sedit, SIGNAL(valueChanged(
int)),
1126 SLOT(slotHSVChanged()));
1128 modeButton =
new QRadioButton(
i18nc(
"This is the V of HSV",
"Value:"), page);
1129 l_lbot->addWidget(modeButton, 2, 0);
1133 d->vedit->setMaximum(255);
1134 l_lbot->addWidget(d->vedit, 2, 1);
1135 connect(d->vedit, SIGNAL(valueChanged(
int)),
1136 SLOT(slotHSVChanged()));
1142 modeButton =
new QRadioButton(
i18n(
"Red:"), page);
1143 l_lbot->addWidget(modeButton, 0, 3);
1144 d->modeGroup->addButton(modeButton,
ChooserRed);
1147 d->redit->setMaximum(255);
1148 l_lbot->addWidget(d->redit, 0, 4);
1149 connect(d->redit, SIGNAL(valueChanged(
int)),
1150 SLOT(slotRGBChanged()));
1152 modeButton =
new QRadioButton(
i18n(
"Green:"), page);
1153 l_lbot->addWidget(modeButton, 1, 3);
1157 d->gedit->setMaximum(255);
1158 l_lbot->addWidget(d->gedit, 1, 4);
1159 connect(d->gedit, SIGNAL(valueChanged(
int)),
1160 SLOT(slotRGBChanged()));
1162 modeButton =
new QRadioButton(
i18n(
"Blue:"), page);
1163 l_lbot->addWidget(modeButton, 2, 3);
1167 d->bedit->setMaximum(255);
1168 l_lbot->addWidget(d->bedit, 2, 4);
1169 connect(d->bedit, SIGNAL(valueChanged(
int)),
1170 SLOT(slotRGBChanged()));
1172 d->alphaLabel =
new KHBox(page);
1174 label =
new QLabel(
i18n(
"Alpha:"), d->alphaLabel);
1175 QStyleOptionButton option;
1176 option.initFrom(modeButton);
1177 QRect labelRect = modeButton->style()->subElementRect(QStyle::SE_RadioButtonContents, &option, modeButton);
1178 int indent = layoutDirection() == Qt::LeftToRight ? labelRect.left() : modeButton->geometry().right() - labelRect.right();
1179 spacer->setFixedWidth(
indent);
1180 l_lbot->addWidget(d->alphaLabel, 3, 3);
1183 d->aedit->setMaximum(255);
1184 label->setBuddy(d->aedit);
1185 l_lbot->addWidget(d->aedit, 3, 4);
1186 connect(d->aedit, SIGNAL(valueChanged(
int)),
1187 SLOT(slotAlphaChanged()));
1189 d->aedit->setVisible(
false);
1190 d->alphaLabel->setVisible(
false);
1191 d->alphaSelector->setVisible(
false);
1196 d->l_right =
new QVBoxLayout;
1197 tl_layout->addLayout(d->l_right, 0, 2);
1202 d->table =
new KColorTable(page);
1203 d->l_right->addWidget(d->table, 10);
1206 SLOT(slotColorSelected(QColor,QString)));
1210 SIGNAL(colorDoubleClicked(QColor,QString)),
1211 SLOT(slotColorDoubleClicked(QColor,QString))
1214 d->originalPalette = d->table->name();
1219 d->l_right->addSpacing(10);
1221 QHBoxLayout *l_hbox =
new QHBoxLayout();
1222 d->l_right->addItem(l_hbox);
1228 addButton->setText(
i18n(
"&Add to Custom Colors"));
1229 l_hbox->addWidget(addButton, 0, Qt::AlignLeft);
1230 connect(addButton, SIGNAL(clicked()), SLOT(slotAddToCustomColors()));
1236 button->setIcon(
KIcon(
"color-picker"));
1237 int commonHeight = addButton->sizeHint().height();
1238 button->setFixedSize(commonHeight, commonHeight);
1239 l_hbox->addWidget(button, 0, Qt::AlignHCenter);
1240 connect(button, SIGNAL(clicked()), SLOT(slotColorPicker()));
1245 d->l_right->addSpacing(10);
1250 QGridLayout *l_grid =
new QGridLayout();
1251 d->l_right->addLayout(l_grid);
1253 l_grid->setColumnStretch(2, 1);
1255 label =
new QLabel(page);
1256 label->setText(
i18n(
"Name:"));
1257 l_grid->addWidget(label, 0, 1, Qt::AlignLeft);
1259 d->colorName =
new QLabel(page);
1260 l_grid->addWidget(d->colorName, 0, 2, Qt::AlignLeft);
1262 label =
new QLabel(page);
1263 label->setText(
i18n(
"HTML:"));
1264 l_grid->addWidget(label, 1, 1, Qt::AlignLeft);
1267 d->htmlName->setMaxLength(13);
1268 d->htmlName->setText(
"#FFFFFF");
1269 int w = d->htmlName->fontMetrics().width(QLatin1String(
"#DDDDDDD"));
1270 d->htmlName->setFixedWidth(w);
1271 l_grid->addWidget(d->htmlName, 1, 2, Qt::AlignLeft);
1273 connect(d->htmlName, SIGNAL(textChanged(QString)),
1274 SLOT(slotHtmlChanged()));
1277 d->patch->setFixedSize(48, 48);
1278 l_grid->addWidget(d->patch, 0, 0, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
1279 connect(d->patch, SIGNAL(colorChanged(QColor)),
1285 setTabOrder(d->hedit, d->sedit);
1286 setTabOrder(d->sedit, d->vedit);
1287 setTabOrder(d->vedit, d->redit);
1288 setTabOrder(d->redit, d->gedit);
1289 setTabOrder(d->gedit, d->bedit);
1290 setTabOrder(d->bedit, d->aedit);
1292 tl_layout->activate();
1293 page->setMinimumSize(page->sizeHint());
1296 d->bRecursion =
false;
1297 d->bEditHsv =
false;
1298 d->bEditRgb =
false;
1299 d->bEditHtml =
false;
1303 col.setHsv(0, 0, 255);
1310 d->hsSelector->setAcceptDrops(
true);
1318 if (d->bColorPicking &&
kapp)
1319 kapp->removeX11EventFilter(d->filter);
1327 if ((obj == d->htmlName) || (obj == d->hsSelector))
1328 switch (ev->type()) {
1329 case QEvent::DragEnter:
1330 case QEvent::DragMove:
1331 case QEvent::DragLeave:
1333 case QEvent::DragResponse:
1334 qApp->sendEvent(d->patch, ev);
1339 return KDialog::eventFilter(obj, ev);
1345 if (!d->cbDefaultColor) {
1349 d->l_right->addSpacing(10);
1354 d->cbDefaultColor =
new QCheckBox(
i18n(
"Default color"),
mainWidget());
1356 d->l_right->addWidget(d->cbDefaultColor);
1358 mainWidget()->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
1359 d->tl_layout->activate();
1363 connect(d->cbDefaultColor, SIGNAL(clicked()), SLOT(slotDefaultColorClicked()));
1366 d->defaultColor = col;
1368 d->slotDefaultColorClicked();
1373 return d->defaultColor;
1378 if (d->bAlphaEnabled != alpha) {
1379 d->bAlphaEnabled = alpha;
1380 d->aedit->setVisible(d->bAlphaEnabled);
1381 d->alphaLabel->setVisible(d->bAlphaEnabled);
1382 d->alphaSelector->setVisible(d->bAlphaEnabled);
1384 mainWidget()->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
1385 d->tl_layout->activate();
1393 return d->bAlphaEnabled;
1400 hsSelector->setChooserMode(c);
1401 valuePal->setChooserMode(c);
1403 modeGroup->button(valuePal->chooserMode())->setChecked(
true);
1404 valuePal->updateContents();
1405 hsSelector->updateContents();
1407 hsSelector->update();
1417void KColorDialog::KColorDialogPrivate::slotDefaultColorClicked()
1419 if (cbDefaultColor->isChecked()) {
1420 selColor = defaultColor;
1421 showColor(selColor,
i18n(
"-default-"));
1423 showColor(selColor, QString());
1425 emit q->colorSelected(selColor);
1429KColorDialog::KColorDialogPrivate::slotModeChanged(
int id)
1435KColorDialog::readSettings()
1439 QString collectionName =
group.readEntry(
"CurrentPalette");
1440 if (collectionName.isEmpty()) {
1451 d->table->setColors(collectionName);
1455KColorDialog::KColorDialogPrivate::slotWriteSettings()
1459 QString collectionName = table->name();
1460 if (!
group.hasDefault(
"CurrentPalette") && table->name() == originalPalette) {
1461 group.revertToDefault(
"CurrentPalette");
1463 QString collectionName(table->name());
1470 group.writeEntry(
"CurrentPalette", collectionName);
1477 if (d->cbDefaultColor && d->cbDefaultColor->isChecked())
1479 if (d->selColor.isValid())
1480 d->table->addToRecentColors(d->selColor);
1495 dlg.setObjectName(
"Color Selector");
1496 if (theColor.isValid())
1498 int result = dlg.exec();
1500 if (result == Accepted) {
1501 theColor = dlg.
color();
1513 dlg.setObjectName(
"Color Selector");
1516 int result = dlg.exec();
1518 if (result == Accepted)
1519 theColor = dlg.
color();
1524void KColorDialog::KColorDialogPrivate::slotRGBChanged(
void)
1526 if (bRecursion)
return;
1527 int red = redit->value();
1528 int grn = gedit->value();
1529 int blu = bedit->value();
1531 if (red > 255 || red < 0)
return;
1532 if (grn > 255 || grn < 0)
return;
1533 if (blu > 255 || blu < 0)
return;
1536 col.setRgb(red, grn, blu, aedit->value());
1542void KColorDialog::KColorDialogPrivate::slotAlphaChanged(
void)
1544 if (bRecursion)
return;
1545 int alpha = aedit->value();
1547 if (alpha > 255 || alpha < 0)
return;
1549 QColor col = selColor;
1550 col.setAlpha(alpha);
1554void KColorDialog::KColorDialogPrivate::slotHtmlChanged(
void)
1556 if (bRecursion || htmlName->text().isEmpty())
return;
1558 QString strColor(htmlName->text());
1561 if (strColor[0] !=
'#') {
1562 bool signalsblocked = htmlName->blockSignals(
true);
1563 strColor.prepend(
"#");
1564 htmlName->setText(strColor);
1565 htmlName->blockSignals(signalsblocked);
1568 const QColor color(strColor);
1570 if (color.isValid()) {
1578void KColorDialog::KColorDialogPrivate::slotHSVChanged(
void)
1580 if (bRecursion)
return;
1581 int hue = hedit->value();
1582 int sat = sedit->value();
1583 int val = vedit->value();
1585 if (hue > 359 || hue < 0)
return;
1586 if (sat > 255 || sat < 0)
return;
1587 if (val > 255 || val < 0)
return;
1590 col.setHsv(hue, sat, val, aedit->value());
1596void KColorDialog::KColorDialogPrivate::slotHSChanged(
int x,
int y)
1598 QColor col = selColor;
1606void KColorDialog::KColorDialogPrivate::slotVChanged(
int v)
1608 QColor col = selColor;
1613void KColorDialog::KColorDialogPrivate::slotAChanged(
int value)
1615 QColor col = selColor;
1616 col.setAlpha(value);
1620void KColorDialog::KColorDialogPrivate::slotColorSelected(
const QColor &color)
1625void KColorDialog::KColorDialogPrivate::slotAddToCustomColors()
1627 table->addToCustomColors(selColor);
1630void KColorDialog::KColorDialogPrivate::slotColorSelected(
const QColor &color,
const QString &name)
1632 _setColor(color, name);
1635void KColorDialog::KColorDialogPrivate::slotColorDoubleClicked
1637 const QColor & color,
1638 const QString & name
1641 _setColor(color, name);
1645void KColorDialog::KColorDialogPrivate::_setColor(
const QColor &color,
const QString &name)
1647 if (color.isValid()) {
1648 if (cbDefaultColor && cbDefaultColor->isChecked())
1649 cbDefaultColor->setChecked(
false);
1652 if (cbDefaultColor && cbDefaultColor->isChecked())
1653 cbDefaultColor->setChecked(
true);
1654 selColor = defaultColor;
1657 showColor(selColor, name);
1659 emit q->colorSelected(selColor);
1663void KColorDialog::KColorDialogPrivate::showColor(
const QColor &color,
const QString &name)
1668 colorName->setText(
i18n(
"-unnamed-"));
1670 colorName->setText(name);
1672 patch->setColor(color);
1677 aedit->setValue(color.alpha());
1679 QColor rgbColor = color.toRgb();
1680 bool ltr = q->layoutDirection() == Qt::LeftToRight;
1681 rgbColor.setAlpha(ltr ? 0 : 255);
1682 alphaSelector->setFirstColor(rgbColor);
1683 rgbColor.setAlpha(ltr ? 255 : 0);
1684 alphaSelector->setSecondColor(rgbColor);
1685 alphaSelector->setValue(color.alpha());
1692 hsSelector->setValues(xValue, yValue);
1693 valuePal->setValue(value);
1695 bool blocked = valuePal->blockSignals(
true);
1697 valuePal->setHue(color.hue());
1698 valuePal->setSaturation(color.saturation());
1699 valuePal->setColorValue(color.value());
1700 valuePal->updateContents();
1701 valuePal->blockSignals(blocked);
1704 blocked = hsSelector->blockSignals(
true);
1706 hsSelector->setHue(color.hue());
1707 hsSelector->setSaturation(color.saturation());
1708 hsSelector->setColorValue(color.value());
1709 hsSelector->updateContents();
1710 hsSelector->blockSignals(blocked);
1711 hsSelector->update();
1719KColorDialog::KColorDialogPrivate::slotColorPicker()
1721 bColorPicking =
true;
1723 filter =
new KCDPickerFilter(q);
1724 kapp->installX11EventFilter(filter);
1726 q->grabMouse(Qt::CrossCursor);
1733 if (d->bColorPicking) {
1734 d->_setColor(
grabColor(e->globalPos()));
1738 KDialog::mouseMoveEvent(e);
1744 if (d->bColorPicking) {
1745 d->bColorPicking =
false;
1747 kapp->removeX11EventFilter(d->filter);
1748 delete d->filter; d->filter = 0;
1752 d->_setColor(
grabColor(e->globalPos()));
1755 KDialog::mouseReleaseEvent(e);
1765 if( !qApp->desktop()->geometry().contains( p ))
1767 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
1768 XImage *ximg = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
1769 unsigned long xpixel = XGetPixel(ximg, 0, 0);
1770 XDestroyImage(ximg);
1772 xcol.pixel = xpixel;
1773 xcol.flags = DoRed | DoGreen | DoBlue;
1774 XQueryColor(QX11Info::display(),
1775 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
1777 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
1779 QWidget *desktop = QApplication::desktop();
1780 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
1781 QImage i = pm.toImage();
1782 return i.pixel(0, 0);
1789 if (d->bColorPicking) {
1790 if (e->key() == Qt::Key_Escape) {
1791 d->bColorPicking =
false;
1793 kapp->removeX11EventFilter(d->filter);
1794 delete d->filter; d->filter = 0;
1805void KColorDialog::KColorDialogPrivate::setRgbEdit(
const QColor &col)
1807 if (bEditRgb)
return;
1809 col.getRgb(&r, &g, &b);
1816void KColorDialog::KColorDialogPrivate::setHtmlEdit(
const QColor &col)
1818 if (bEditHtml)
return;
1820 col.getRgb(&r, &g, &b);
1823 num.sprintf(
"#%02X%02X%02X", r, g, b);
1824 htmlName->setText(num);
1828void KColorDialog::KColorDialogPrivate::setHsvEdit(
const QColor &col)
1830 if (bEditHsv)
return;
1832 col.getHsv(&h, &s, &v);
1839#include "kcolordialog.moc"
1840#include "kcolordialog_p.moc"
A table of editable color cells.
void setAcceptDrags(bool acceptDrags)
void colorDoubleClicked(int index, const QColor &color)
Emitted when a color in the table is double-clicked.
virtual void mouseReleaseEvent(QMouseEvent *)
virtual void mousePressEvent(QMouseEvent *)
void setSelected(int index)
Sets the currently selected cell to index.
virtual void mouseDoubleClickEvent(QMouseEvent *)
QColor color(int index) const
Returns the color at a given index in the table.
KColorCells(QWidget *parent, int rows, int columns)
Constructs a new table of color cells, consisting of rows * columns colors.
void colorSelected(int index, const QColor &color)
Emitted when a color is selected in the table.
void setShading(bool shade)
int positionToCell(const QPoint &pos, bool ignoreBorders=false) const
virtual void mouseMoveEvent(QMouseEvent *)
int count() const
Returns the total number of color cells in the table.
virtual int sizeHintForRow(int column) const
virtual void dragEnterEvent(QDragEnterEvent *)
virtual int sizeHintForColumn(int column) const
int selectedIndex() const
Returns the index of the cell which is currently selected.
virtual void resizeEvent(QResizeEvent *event)
void setColor(int index, const QColor &col)
Sets the color in the given index in the table.
virtual void dragMoveEvent(QDragMoveEvent *)
virtual void dropEvent(QDropEvent *)
Class for handling color collections ("palettes").
static QStringList installedCollections()
Query which KDE color collections are installed.
int findColor(const QColor &color) const
Find index by color.
int addColor(const QColor &newColor, const QString &newColorName=QString())
Add a color.
bool save()
Save the collection.
A color selection dialog.
virtual void mouseReleaseEvent(QMouseEvent *)
void setAlphaChannelEnabled(bool alpha)
When set to true, the user is allowed to change the alpha component of the color.
static int getColor(QColor &theColor, QWidget *parent=0L)
Creates a modal color dialog, let the user choose a color, and returns when the dialog is closed.
static QColor grabColor(const QPoint &p)
Gets the color from the pixel at point p on the screen.
KColorDialog(QWidget *parent=0L, bool modal=false)
Constructs a color selection dialog.
void colorSelected(const QColor &col)
Emitted when a color is selected.
~KColorDialog()
Destroys the color selection dialog.
void setColor(const QColor &col)
Preselects a color.
bool isAlphaChannelEnabled
virtual void mouseMoveEvent(QMouseEvent *)
virtual bool eventFilter(QObject *obj, QEvent *ev)
virtual void keyPressEvent(QKeyEvent *)
void setDefaultColor(const QColor &defaultCol)
Call this to make the dialog show a "Default Color" checkbox.
void colorChanged(const QColor &)
This signal is emitted whenever the current color changes due to a drop event.
virtual void mouseMoveEvent(QMouseEvent *)
virtual void dropEvent(QDropEvent *)
virtual void paintEvent(QPaintEvent *pe)
void setColor(const QColor &col)
Set the color to display and update the display.
KColorPatch(QWidget *parent)
virtual void dragEnterEvent(QDragEnterEvent *)
A dialog base class with standard buttons and predefined layouts.
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
KPushButton * button(ButtonCode id) const
Returns the button that corresponds to the id.
virtual QSize sizeHint() const
Reimplemented from QDialog.
void closeClicked()
The Close button was pressed.
virtual void keyPressEvent(QKeyEvent *)
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
@ Close
Show Close-button. (this button closes the dialog)
void buttonClicked(KDialog::ButtonCode button)
A button has been pressed.
void okClicked()
The OK button was pressed.
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
static int dndEventDelay()
Returns a threshold in pixels for drag & drop operations.
The KGradientSelector widget allows the user to choose from a one-dimensional range of colors which i...
A container widget which arranges its children horizontally.
A wrapper around QIcon that provides KDE icon features.
A QSpinBox with support for arbitrary base numbers.
An enhanced QLineEdit widget for inputting text.
void insertCatalog(const QString &catalog)
static void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Display an "Sorry" dialog.
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
static const ColorCollectionNameType colorCollectionName[]
static const char *const * namedColorFilePath(void)
QString indent(QString text, int spaces)
QString i18n(const char *text)
QString i18nc(const char *ctxt, const char *text)
QString i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1)
#define I18N_NOOP2(comment, x)
We need to remember the context to get the correct translation.
QDrag * createDrag(const QColor &color, QWidget *dragsource)
Creates a color drag object.
QColor fromMimeData(const QMimeData *mimeData)
Decodes the MIME data mimeData and returns the resulting color.
bool canDecode(const QMimeData *mimeData)
Returns true if the MIME data mimeData contains a color object.
QColor shade(const QColor &, qreal lumaAmount, qreal chromaAmount=0.0)
Adjust the luma and chroma components of a color.
qreal contrastRatio(const QColor &, const QColor &)
Calculate the contrast ratio between two colors, according to the W3C/WCAG2.0 algorithm,...
qreal getComponentValue(const QColor &color, KColorChooserMode chooserMode)
void setComponentValue(QColor &color, KColorChooserMode chooserMode, qreal value)
void fillOpaqueRect(QPainter *painter, const QRect &rect, const QBrush &brush)
KSharedConfigPtr config()
const char * name(StandardAction id)
This will return the internal name of a given standard action.