Subversion Repositories Scribus

Rev

Rev 2986 | Rev 3331 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1809 subik 1
/* $Id: cwdialog.cpp 3107 2005-08-28 13:47:16Z subik $ */
2
#include "cwdialog.h"
3
#include "cwdialog.moc"
4
 
5
#include <qvariant.h>
6
#include <qcombobox.h>
7
#include <qheader.h>
8
#include <qlistview.h>
9
#include <qlayout.h>
10
#include <qtooltip.h>
11
#include <qwhatsthis.h>
12
#include <qpushbutton.h>
13
#include <qspinbox.h>
14
#include <qpainter.h>
15
 
2856 cbradney 16
#include "prefsmanager.h"
17
#include "prefsfile.h"
18
#include "mpalette.h"
3107 subik 19
#include "colorblind.h"
1809 subik 20
 
3107 subik 21
 
2986 craig 22
extern ScribusApp SCRIBUS_API *ScApp;
1809 subik 23
 
24
ColorWheelDialog::ColorWheelDialog(QWidget* parent, const char* name, bool modal, WFlags fl)
25
        : QDialog(parent, name, modal, fl)
26
{
27
        if (!name)
28
                setName("ColorWheelDialog");
29
 
30
        formLayout = new QGridLayout(this, 1, 1, 11, 6, "formLayout");
31
        mainLayout = new QHBoxLayout(0, 0, 6, "mainLayout");
32
        wheelLayout = new QVBoxLayout(0, 0, 6, "wheelLayout");
33
 
34
        colorWheel = new ColorWheel(this, "colorWheel");
35
        colorWheel->setFrameShape(QFrame::Box);
36
        colorWheel->setMinimumSize(QSize(300, 300));
37
        colorWheel->setMaximumSize(QSize(300, 300));
38
        wheelLayout->addWidget(colorWheel);
39
 
2911 subik 40
        defectLayout = new QHBoxLayout(0, 0, 6, "defectLayout");
41
        defectLabel = new QLabel(this, "defectLabel");
42
        defectLayout->addWidget(defectLabel);
43
        defectCombo = new QComboBox(false, this, "defectCombo");
44
        defectLayout->addWidget(defectCombo);
45
        wheelLayout->addLayout(defectLayout);
46
 
1809 subik 47
        previewLabel = new QLabel(this, "previewLabel");
48
        previewLabel->setFrameShape(QFrame::Box);
49
        previewLabel->setMinimumSize(QSize(300, 120));
50
        previewLabel->setMaximumSize(QSize(300, 120));
51
        wheelLayout->addWidget(previewLabel);
52
 
53
        spacer1 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
54
        wheelLayout->addItem(spacer1);
55
        mainLayout->addLayout(wheelLayout);
56
 
57
        listLayout = new QVBoxLayout(0, 0, 6, "listLayout");
58
 
59
        typeLabel = new QLabel(this, "typeLabel");
60
        listLayout->addWidget(typeLabel);
1812 subik 61
        typeCombo = new QComboBox(false, this, "typeCombo");
1809 subik 62
        listLayout->addWidget(typeCombo);
63
 
64
        angleLabel = new QLabel(this, "angleLabel");
65
        angleLayout = new QHBoxLayout(0, 0, 6, "angleLayout");
66
        angleLayout->addWidget(angleLabel);
67
 
68
        angleSpin = new QSpinBox(this, "angleSpin");
69
        angleSpin->setValue(15);
70
        angleSpin->setMinValue(0);
1812 subik 71
        angleSpin->setMaxValue(90);
1809 subik 72
        angleLayout->addWidget(angleSpin);
73
        listLayout->addLayout(angleLayout);
74
 
75
        colorList = new QListView(this, "colorList");
76
        listLayout->addWidget(colorList);
77
 
78
        buttonLayout = new QHBoxLayout(0, 0, 6, "buttonLayout");
79
        addButton = new QPushButton(this, "addButton");
80
        buttonLayout->addWidget(addButton);
1811 subik 81
        replaceButton = new QPushButton(this, "replaceButton");
82
        buttonLayout->addWidget(replaceButton);
1809 subik 83
        cancelButton = new QPushButton(this, "cancelButton");
84
        buttonLayout->addWidget(cancelButton);
85
 
86
        listLayout->addLayout(buttonLayout);
87
        mainLayout->addLayout(listLayout);
88
        formLayout->addLayout(mainLayout, 0, 0);
89
 
90
        languageChange();
91
        resize(QSize(600, 480).expandedTo(minimumSizeHint()));
92
        clearWState(WState_Polished);
93
 
94
        // setup combobox
95
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Monochromatic), colorWheel->Monochromatic);
96
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Analogous), colorWheel->Analogous);
97
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Complementary), colorWheel->Complementary);
98
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Split), colorWheel->Split);
99
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Triadic), colorWheel->Triadic);
100
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Tetradic), colorWheel->Tetradic);
2911 subik 101
        // defects
102
        defectCombo->insertItem(tr("Normal Vision"));
3107 subik 103
        defectCombo->insertItem(tr("Protanopia (red)"));
104
        defectCombo->insertItem(tr("Deuteranopia (green)"));
105
        defectCombo->insertItem(tr("Tritanopia (blue)"));
2911 subik 106
        defectCombo->insertItem(tr("Full Color Blindness"));
1809 subik 107
 
108
        // preferences
2856 cbradney 109
        prefs = PrefsManager::instance()->prefsFile->getPluginContext("colorwheel");
1809 subik 110
        typeCombo->setCurrentItem(prefs->getInt("cw_type", 0));
111
        angleSpin->setValue(prefs->getInt("cw_angle", 15));
112
        colorWheel->angle = angleSpin->value();
113
        QValueVector<QPoint> vp;
1811 subik 114
        int x = prefs->getInt("cw_x", colorWheel->width()/2 + 1);
115
        int y = prefs->getInt("cw_y", colorWheel->height()/2);
1809 subik 116
        vp.append(QPoint(x, y));
117
        colorWheel->actualPoint = QPoint(x, y);
118
        colorWheel->actualRgb = QColor(prefs->getInt("cw_r", 0), prefs->getInt("cw_g", 0), prefs->getInt("cw_b", 0)).rgb();
119
        colorWheel->paintWheel(vp);
120
 
121
        // actions
122
        typeCombo_activated(typeCombo->currentItem());
123
        // signals and slots connections
124
        connect(typeCombo, SIGNAL(activated(int)), this, SLOT(typeCombo_activated(int)));
2911 subik 125
        connect(defectCombo, SIGNAL(activated(int)), this, SLOT(defectCombo_activated(int)));
1809 subik 126
        connect(colorWheel, SIGNAL(clicked(int, const QPoint&)), this, SLOT(colorWheel_clicked(int, const QPoint&)));
127
        colorWheel_clicked(0, QPoint(0, 0));
128
        connect(angleSpin, SIGNAL(valueChanged(int)), this, SLOT(angleSpin_valueChanged(int)));
129
        connect(addButton, SIGNAL(clicked()), this, SLOT(addButton_clicked()));
1811 subik 130
        connect(replaceButton, SIGNAL(clicked()), this, SLOT(replaceButton_clicked()));
1809 subik 131
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelButton_clicked()));
132
}
133
 
134
/*
135
 *  Destroys the object and frees any allocated resources
136
 */
137
ColorWheelDialog::~ColorWheelDialog()
138
{
139
        // no need to delete child widgets, Qt does it all for us
140
        // preferences
141
        prefs->set("cw_type", typeCombo->currentItem());
142
        prefs->set("cw_angle", angleSpin->value());
143
        prefs->set("cw_x", colorWheel->actualPoint.x());
144
        prefs->set("cw_y", colorWheel->actualPoint.y());
145
        prefs->set("cw_r", qRed(colorWheel->actualRgb));
146
        prefs->set("cw_g", qGreen(colorWheel->actualRgb));
147
        prefs->set("cw_b", qBlue(colorWheel->actualRgb));
148
}
149
 
150
/*
151
 *  Sets the strings of the subwidgets using the current
152
 *  language.
153
 */
154
void ColorWheelDialog::languageChange()
155
{
2911 subik 156
        defectLabel->setText(tr("Vision Defect:"));
1809 subik 157
        setCaption(tr("Color Wheel"));
158
        colorList->addColumn(tr("Color"));
159
        colorList->addColumn(tr("Name"));
160
        colorList->addColumn(tr("C"));
161
        colorList->addColumn(tr("M"));
162
        colorList->addColumn(tr("Y"));
163
        colorList->addColumn(tr("K"));
164
        colorList->setSorting(1);
165
        typeLabel->setText(tr("Select Method:"));
1812 subik 166
        angleLabel->setText(tr("Angle (0 - 90 degrees):"));
1811 subik 167
        addButton->setText(tr("&Merge Colors"));
168
        replaceButton->setText(tr("&Replace Colors"));
1809 subik 169
        cancelButton->setText(tr("&Cancel"));
170
        // tips
1811 subik 171
        QToolTip::add(addButton, "<qt>" + tr("Merge created colors into the document colors") + "</qt>");
172
        QToolTip::add(addButton, "<qt>" + tr("Replace created colors in the document colors") + "</qt>");
1809 subik 173
        QToolTip::add(cancelButton, "<qt>" + tr("Leave colors untouched") + "</qt>");
174
        QToolTip::add(angleSpin, "<qt>" + tr("Difference between selected value and counted ones. See documentation for more info") + "</qt>");
175
        QToolTip::add(colorWheel, "<qt>" + tr("Click the wheel to get base color") + "</qt>");
176
        QToolTip::add(previewLabel, "<qt>" + tr("Here you have the sample color schema") + "</qt>");
177
        QToolTip::add(typeCombo, "<qt>" + tr("Select one of the method to create color schema. See documentation for more info") + "</qt>");
178
        QToolTip::add(colorList, "<qt>" + tr("Here you have the color of your chosen color schema") + "</qt>");
2911 subik 179
        QToolTip::add(defectCombo, "<qt>" + tr("You can simulate common vision defects here. Just select type of the defect") + "</qt>");
1809 subik 180
}
181
 
182
void ColorWheelDialog::fillColorList()
183
{
184
        colorList->clear();
185
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
186
        {
187
                int c, m, y, k;
188
                QListViewItem *item = new QListViewItem(colorList);
189
                item->setPixmap(0, colorWheel->sample(it.data().getRGBColor()));
190
                item->setText(1, it.key());
191
                it.data().getCMYK(&c, &m, &y, &k);
192
                item->setText(2, QString("%1").arg(c));
193
                item->setText(3, QString("%1").arg(m));
194
                item->setText(4, QString("%1").arg(y));
195
                item->setText(5, QString("%1").arg(k));
196
        }
197
}
198
 
199
void ColorWheelDialog::typeCombo_activated(int index)
200
{
201
        colorList->clear();
202
        if (index == colorWheel->Monochromatic)
203
        {
204
                angleSpin->setEnabled(false);
205
                angleLabel->setEnabled(false);
206
                colorWheel->makeMonochromatic();
207
        }
208
        if (index == colorWheel->Analogous)
209
        {
210
                angleSpin->setEnabled(true);
211
                angleLabel->setEnabled(true);
212
                colorWheel->makeAnalogous();
213
        }
214
        if (index == colorWheel->Complementary)
215
        {
216
                angleSpin->setEnabled(false);
217
                angleLabel->setEnabled(false);
218
                colorWheel->makeComplementary();
219
        }
220
        if (index == colorWheel->Split)
221
        {
222
                angleSpin->setEnabled(true);
223
                angleLabel->setEnabled(true);
224
                colorWheel->makeSplit();
225
        }
226
        if (index == colorWheel->Triadic)
227
        {
228
                angleSpin->setEnabled(false); //TODO
229
                angleLabel->setEnabled(false);
230
                colorWheel->makeTriadic();
231
        }
232
        if (index == colorWheel->Tetradic)
233
        {
1812 subik 234
                angleSpin->setEnabled(true);
235
                angleLabel->setEnabled(true);
1809 subik 236
                colorWheel->makeTetradic();
237
        }
238
        fillColorList();
239
        setPreview();
240
}
241
 
242
void ColorWheelDialog::colorWheel_clicked(int, const QPoint&)
243
{
244
        typeCombo_activated(typeCombo->currentItem());
245
}
246
 
247
void ColorWheelDialog::angleSpin_valueChanged(int value)
248
{
249
        colorWheel->angle = value;
250
        typeCombo_activated(typeCombo->currentItem());
251
}
252
 
253
void ColorWheelDialog::addButton_clicked()
254
{
255
        QString status("<qt><h2>" + tr("Merging colors") + "</h2><p>");
1811 subik 256
        bool err = false;
1809 subik 257
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
258
        {
259
                if (ScApp->doc->PageColors.contains(it.key()))
1811 subik 260
                {
1809 subik 261
                        status += "<b>" + tr("Error: ") + "</b>" + tr(QString("Color %1 exists already!").arg(it.key())) + "<br/>";
1811 subik 262
                        err = true;
263
                }
1809 subik 264
                else
265
                {
266
                        status += tr(QString("Color %1 appended.").arg(it.key())) + "<br/>";
267
                        ScApp->doc->PageColors[it.key()] = it.data();
268
                }
269
        }
1811 subik 270
        status += "<p>" + tr("Now opening the color manager.") + "</p></qt>";
271
        if (err)
272
        {
273
                QMessageBox::information(this, tr("Color Merging"), status);
274
                ScApp->slotEditColors();
275
                return;
276
        }
277
        ScApp->propertiesPalette->Cpal->SetColors(ScApp->doc->PageColors);
278
        ScApp->propertiesPalette->updateCList();
1809 subik 279
        accept();
280
}
281
 
1811 subik 282
void ColorWheelDialog::replaceButton_clicked()
283
{
284
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
285
        {
286
                ScApp->doc->PageColors[it.key()] = it.data();
287
        }
288
        ScApp->propertiesPalette->Cpal->SetColors(ScApp->doc->PageColors);
289
        ScApp->propertiesPalette->updateCList();
290
        accept();
291
}
1809 subik 292
 
293
void ColorWheelDialog::cancelButton_clicked()
294
{
295
        reject();
296
}
297
 
2911 subik 298
void ColorWheelDialog::defectCombo_activated(int /*index*/)
299
{
300
        setPreview();
301
}
302
 
1809 subik 303
void ColorWheelDialog::setPreview()
304
{
305
        int x = previewLabel->width();
306
        int y = previewLabel->height();
2886 fschmid 307
        QValueList<ScColor> cols = colorWheel->colorList.values();
1809 subik 308
        int xstep = x / cols.count();
309
        QPixmap pm = QPixmap(x, y);
310
        QPainter *p = new QPainter(&pm);
311
        QFontMetrics fm = p->fontMetrics();
312
 
313
        pm.fill(Qt::white);
314
        p->setPen(Qt::white);
315
        p->drawRect(0, 0, x, y);
316
        for (uint i = 0; i < cols.count(); ++i)
317
        {
2911 subik 318
                QColor c = computeDefect(cols[i].getRGBColor());
319
                p->setPen(c);
320
                p->setBrush(c);
1809 subik 321
                p->drawRect(i * xstep, 0, xstep, y);
322
        }
323
        p->setPen(Qt::black);
324
        p->setBrush(Qt::black);
325
        p->drawText(15, 5 + fm.height(), "Lorem ipsum dolor sit amet");
326
        p->setPen(Qt::white);
327
        p->setBrush(Qt::white);
328
        p->drawText(25, y - 5 - fm.height(), "Lorem ipsum dolor sit amet");
329
        p->end();
330
        delete(p);
331
        previewLabel->clear();
332
        previewLabel->setPixmap(pm);
333
}
2911 subik 334
 
335
QColor ColorWheelDialog::computeDefect(QColor c)
336
{
3107 subik 337
        if (defectCombo->currentItem() == VisionDefectColor::normalVision)
2911 subik 338
                return c;
3107 subik 339
        VisionDefectColor *defect = new VisionDefectColor(c);
340
        defect->deficiency = defectCombo->currentItem();
341
        defect->convertDefect();
342
        QColor nc = defect->getColor();
343
        delete defect;
344
        return nc;
2911 subik 345
}
346