Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
1809 subik 7
/* $Id: cwdialog.cpp 5915 2006-07-15 10:36:20Z fschmid $ */
8
#include "cwdialog.h"
9
#include "cwdialog.moc"
10
 
11
#include <qvariant.h>
12
#include <qcombobox.h>
13
#include <qheader.h>
14
#include <qlistview.h>
4311 subik 15
#include <qlistbox.h>
1809 subik 16
#include <qlayout.h>
17
#include <qtooltip.h>
18
#include <qwhatsthis.h>
19
#include <qpushbutton.h>
20
#include <qspinbox.h>
21
#include <qpainter.h>
3331 subik 22
#include <qmenubar.h>
3709 subik 23
#include <qgroupbox.h>
24
#include <qslider.h>
1809 subik 25
 
2856 cbradney 26
#include "prefsmanager.h"
3331 subik 27
#include "commonstrings.h"
28
#include "cmykfw.h"
2856 cbradney 29
#include "prefsfile.h"
30
#include "mpalette.h"
3107 subik 31
#include "colorblind.h"
3709 subik 32
#include "cwsetcolor.h"
3964 subik 33
#include "util.h"
4311 subik 34
#include "colorm.h"
1809 subik 35
 
3107 subik 36
 
5781 cbradney 37
ScribusColorList::ScribusColorList(ScribusDoc* doc, QWidget* parent, const char* name, bool modal, WFlags fl)
38
        : QDialog(parent, name, modal, fl),
39
        m_Doc(doc)
3331 subik 40
{
41
        if (!name)
42
                setName("ScribusColorList");
43
        ScribusColorListLayout = new QGridLayout(this, 1, 1, 11, 6, "ScribusColorListLayout");
44
 
45
        listLayout = new QVBoxLayout(0, 0, 6, "listLayout");
46
 
4311 subik 47
        listView = new ColorListBox(this, "listView");
3331 subik 48
        listLayout->addWidget(listView);
5781 cbradney 49
        listView->updateBox(m_Doc->PageColors, ColorListBox::fancyPixmap);
3331 subik 50
 
51
        btnLayout = new QHBoxLayout(0, 0, 6, "btnLayout");
52
        btnSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
53
        btnLayout->addItem(btnSpacer);
54
 
55
        okButton = new QPushButton(this, "okButton");
56
        btnLayout->addWidget(okButton);
57
 
58
        cancelButton = new QPushButton(this, "cancelButton");
59
        btnLayout->addWidget(cancelButton);
60
        listLayout->addLayout(btnLayout);
61
 
62
        ScribusColorListLayout->addLayout(listLayout, 0, 0);
63
        languageChange();
64
        resize(QSize(288, 310).expandedTo(minimumSizeHint()));
65
        clearWState(WState_Polished);
66
 
4982 subik 67
        connect(listView, SIGNAL(doubleClicked(QListBoxItem *)), this, SLOT(okButton_clicked()));
3331 subik 68
        connect(okButton, SIGNAL(clicked()), this, SLOT(okButton_clicked()));
69
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
70
}
71
 
72
void ScribusColorList::languageChange()
73
{
74
        setCaption(tr("Document Colors"));
75
        okButton->setText(CommonStrings::tr_OK);
76
        cancelButton->setText(CommonStrings::tr_Cancel);
77
}
78
 
79
void ScribusColorList::okButton_clicked()
80
{
5781 cbradney 81
        ScColor c = m_Doc->PageColors[listView->currentText()];
3331 subik 82
        selectedColor = c.getRGBColor();
83
        accept();
84
}
85
 
86
 
5781 cbradney 87
ColorWheelDialog::ColorWheelDialog(ScribusDoc* doc, QWidget* parent, const char* name, bool modal, WFlags fl)
88
        : QDialog(parent, name, modal, fl),
89
        m_Doc(doc)
1809 subik 90
{
91
        if (!name)
92
                setName("ColorWheelDialog");
93
 
3331 subik 94
        QMenuBar *menuBar = new QMenuBar(this, "menuBar");
95
        QPopupMenu *colorMenu = new QPopupMenu(this);
3353 subik 96
        colorMenu->insertItem(tr("Cr&eate color..."), this, SLOT(createColor()));
3709 subik 97
        colorMenu->insertItem(tr("C&olor Components..."), this, SLOT(setColorComponents()));
3331 subik 98
        colorMenu->insertItem(tr("&Import existing color..."), this, SLOT(importColor()));
3353 subik 99
        colorMenu->insertSeparator();
100
        colorMenu->insertItem(tr("&Merge colors"), this, SLOT(addButton_clicked()));
101
        colorMenu->insertItem(tr("&Replace colors"), this, SLOT(replaceButton_clicked()));
102
        colorMenu->insertItem(tr("E&xit"), this, SLOT(cancelButton_clicked()));
103
        menuBar->insertItem(tr("C&olor"), colorMenu);
3331 subik 104
 
1809 subik 105
        formLayout = new QGridLayout(this, 1, 1, 11, 6, "formLayout");
3331 subik 106
        formLayout->setMenuBar(menuBar);
1809 subik 107
        mainLayout = new QHBoxLayout(0, 0, 6, "mainLayout");
108
        wheelLayout = new QVBoxLayout(0, 0, 6, "wheelLayout");
109
 
110
        colorWheel = new ColorWheel(this, "colorWheel");
111
        colorWheel->setFrameShape(QFrame::Box);
112
        colorWheel->setMinimumSize(QSize(300, 300));
113
        colorWheel->setMaximumSize(QSize(300, 300));
114
        wheelLayout->addWidget(colorWheel);
115
 
116
        typeLabel = new QLabel(this, "typeLabel");
3374 subik 117
        wheelLayout->addWidget(typeLabel);
1812 subik 118
        typeCombo = new QComboBox(false, this, "typeCombo");
3374 subik 119
        wheelLayout->addWidget(typeCombo);
1809 subik 120
 
121
        angleLabel = new QLabel(this, "angleLabel");
122
        angleLayout = new QHBoxLayout(0, 0, 6, "angleLayout");
123
        angleLayout->addWidget(angleLabel);
124
        angleSpin = new QSpinBox(this, "angleSpin");
125
        angleSpin->setMinValue(0);
1812 subik 126
        angleSpin->setMaxValue(90);
1809 subik 127
        angleLayout->addWidget(angleSpin);
3374 subik 128
        wheelLayout->addLayout(angleLayout);
1809 subik 129
 
3374 subik 130
        spacer1 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
131
        wheelLayout->addItem(spacer1);
132
        mainLayout->addLayout(wheelLayout);
133
 
134
        listLayout = new QVBoxLayout(0, 0, 6, "listLayout");
135
 
136
        defectLayout = new QHBoxLayout(0, 0, 6, "defectLayout");
137
        defectLabel = new QLabel(this, "defectLabel");
138
        defectLayout->addWidget(defectLabel);
139
        defectCombo = new QComboBox(false, this, "defectCombo");
140
        defectLayout->addWidget(defectCombo);
141
        listLayout->addLayout(defectLayout);
142
 
143
        previewLabel = new QLabel(this, "previewLabel");
144
        previewLabel->setFrameShape(QFrame::Box);
145
        previewLabel->setMinimumSize(QSize(400, 160));
146
        previewLabel->setMaximumSize(QSize(400, 160));
147
        listLayout->addWidget(previewLabel);
148
 
1809 subik 149
        colorList = new QListView(this, "colorList");
150
        listLayout->addWidget(colorList);
151
 
152
        buttonLayout = new QHBoxLayout(0, 0, 6, "buttonLayout");
153
        addButton = new QPushButton(this, "addButton");
4979 subik 154
        addButton->setAutoDefault(false);
1809 subik 155
        buttonLayout->addWidget(addButton);
1811 subik 156
        replaceButton = new QPushButton(this, "replaceButton");
4979 subik 157
        replaceButton->setAutoDefault(false);
1811 subik 158
        buttonLayout->addWidget(replaceButton);
1809 subik 159
        cancelButton = new QPushButton(this, "cancelButton");
4979 subik 160
        cancelButton->setAutoDefault(false);
1809 subik 161
        buttonLayout->addWidget(cancelButton);
162
 
163
        listLayout->addLayout(buttonLayout);
164
        mainLayout->addLayout(listLayout);
165
        formLayout->addLayout(mainLayout, 0, 0);
166
 
167
        languageChange();
168
        resize(QSize(600, 480).expandedTo(minimumSizeHint()));
169
        clearWState(WState_Polished);
170
 
171
        // setup combobox
172
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Monochromatic), colorWheel->Monochromatic);
173
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Analogous), colorWheel->Analogous);
174
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Complementary), colorWheel->Complementary);
175
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Split), colorWheel->Split);
176
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Triadic), colorWheel->Triadic);
177
        typeCombo->insertItem(colorWheel->getTypeDescription(colorWheel->Tetradic), colorWheel->Tetradic);
2911 subik 178
        // defects
179
        defectCombo->insertItem(tr("Normal Vision"));
3561 cbradney 180
        defectCombo->insertItem(tr("Protanopia (Red)"));
181
        defectCombo->insertItem(tr("Deuteranopia (Green)"));
182
        defectCombo->insertItem(tr("Tritanopia (Blue)"));
2911 subik 183
        defectCombo->insertItem(tr("Full Color Blindness"));
1809 subik 184
 
185
        // preferences
2856 cbradney 186
        prefs = PrefsManager::instance()->prefsFile->getPluginContext("colorwheel");
1809 subik 187
        typeCombo->setCurrentItem(prefs->getInt("cw_type", 0));
188
        angleSpin->setValue(prefs->getInt("cw_angle", 15));
189
        colorWheel->angle = angleSpin->value();
3331 subik 190
        colorWheel->baseAngle = prefs->getInt("cw_baseangle", 0);
191
        colorWheel->actualColor = QColor(prefs->getInt("cw_r", 0), prefs->getInt("cw_g", 0), prefs->getInt("cw_b", 0));
1809 subik 192
 
193
        // actions
194
        typeCombo_activated(typeCombo->currentItem());
3709 subik 195
 
1809 subik 196
        // signals and slots connections
197
        connect(typeCombo, SIGNAL(activated(int)), this, SLOT(typeCombo_activated(int)));
2911 subik 198
        connect(defectCombo, SIGNAL(activated(int)), this, SLOT(defectCombo_activated(int)));
1809 subik 199
        connect(colorWheel, SIGNAL(clicked(int, const QPoint&)), this, SLOT(colorWheel_clicked(int, const QPoint&)));
200
        colorWheel_clicked(0, QPoint(0, 0));
201
        connect(angleSpin, SIGNAL(valueChanged(int)), this, SLOT(angleSpin_valueChanged(int)));
202
        connect(addButton, SIGNAL(clicked()), this, SLOT(addButton_clicked()));
1811 subik 203
        connect(replaceButton, SIGNAL(clicked()), this, SLOT(replaceButton_clicked()));
1809 subik 204
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelButton_clicked()));
205
}
206
 
207
/*
208
 *  Destroys the object and frees any allocated resources
209
 */
210
ColorWheelDialog::~ColorWheelDialog()
211
{
212
        // no need to delete child widgets, Qt does it all for us
213
        // preferences
214
        prefs->set("cw_type", typeCombo->currentItem());
215
        prefs->set("cw_angle", angleSpin->value());
3331 subik 216
        prefs->set("cw_baseangle", colorWheel->baseAngle);
217
        prefs->set("cw_r", colorWheel->actualColor.red());
218
        prefs->set("cw_g", colorWheel->actualColor.green());
219
        prefs->set("cw_b", colorWheel->actualColor.blue());
1809 subik 220
}
221
 
222
/*
223
 *  Sets the strings of the subwidgets using the current
224
 *  language.
225
 */
226
void ColorWheelDialog::languageChange()
227
{
2911 subik 228
        defectLabel->setText(tr("Vision Defect:"));
1809 subik 229
        setCaption(tr("Color Wheel"));
230
        colorList->addColumn(tr("Color"));
231
        colorList->addColumn(tr("Name"));
232
        colorList->addColumn(tr("C"));
233
        colorList->addColumn(tr("M"));
234
        colorList->addColumn(tr("Y"));
235
        colorList->addColumn(tr("K"));
236
        colorList->setSorting(1);
237
        typeLabel->setText(tr("Select Method:"));
1812 subik 238
        angleLabel->setText(tr("Angle (0 - 90 degrees):"));
1811 subik 239
        addButton->setText(tr("&Merge Colors"));
240
        replaceButton->setText(tr("&Replace Colors"));
3353 subik 241
        cancelButton->setText(CommonStrings::tr_Cancel);
1809 subik 242
        // tips
1811 subik 243
        QToolTip::add(addButton, "<qt>" + tr("Merge created colors into the document colors") + "</qt>");
3331 subik 244
        QToolTip::add(replaceButton, "<qt>" + tr("Replace created colors in the document colors") + "</qt>");
1809 subik 245
        QToolTip::add(cancelButton, "<qt>" + tr("Leave colors untouched") + "</qt>");
3354 cbradney 246
        QToolTip::add(angleSpin, "<qt>" + tr("Difference between the selected value and the counted ones. Refer to documentation for more information.") + "</qt>");
247
        QToolTip::add(colorWheel, "<qt>" + tr("Click the wheel to get the base color. It is hue in HSV mode.") + "</qt>");
248
        QToolTip::add(previewLabel, "<qt>" + tr("Sample color scheme") + "</qt>");
249
        QToolTip::add(typeCombo, "<qt>" + tr("Select one of the methods to create a color scheme. Refer to documentation for more information.") + "</qt>");
250
        QToolTip::add(colorList, "<qt>" + tr("Colors of your chosen color scheme") + "</qt>");
251
        QToolTip::add(defectCombo, "<qt>" + tr("Simulate common vision defects here. Select type of the defect.") + "</qt>");
1809 subik 252
}
253
 
254
void ColorWheelDialog::fillColorList()
255
{
256
        colorList->clear();
257
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
258
        {
259
                int c, m, y, k;
260
                QListViewItem *item = new QListViewItem(colorList);
3964 subik 261
                QPixmap *pm = getSmallPixmap(it.data().getRGBColor());
262
                item->setPixmap(0, *pm);
1809 subik 263
                item->setText(1, it.key());
264
                it.data().getCMYK(&c, &m, &y, &k);
265
                item->setText(2, QString("%1").arg(c));
266
                item->setText(3, QString("%1").arg(m));
267
                item->setText(4, QString("%1").arg(y));
268
                item->setText(5, QString("%1").arg(k));
269
        }
270
}
271
 
272
void ColorWheelDialog::typeCombo_activated(int index)
273
{
274
        colorList->clear();
275
        if (index == colorWheel->Monochromatic)
276
        {
277
                angleSpin->setEnabled(false);
278
                angleLabel->setEnabled(false);
279
                colorWheel->makeMonochromatic();
280
        }
281
        if (index == colorWheel->Analogous)
282
        {
283
                angleSpin->setEnabled(true);
284
                angleLabel->setEnabled(true);
285
                colorWheel->makeAnalogous();
286
        }
287
        if (index == colorWheel->Complementary)
288
        {
289
                angleSpin->setEnabled(false);
290
                angleLabel->setEnabled(false);
291
                colorWheel->makeComplementary();
292
        }
293
        if (index == colorWheel->Split)
294
        {
295
                angleSpin->setEnabled(true);
296
                angleLabel->setEnabled(true);
297
                colorWheel->makeSplit();
298
        }
299
        if (index == colorWheel->Triadic)
300
        {
3659 subik 301
                angleSpin->setEnabled(false);
1809 subik 302
                angleLabel->setEnabled(false);
303
                colorWheel->makeTriadic();
304
        }
305
        if (index == colorWheel->Tetradic)
306
        {
1812 subik 307
                angleSpin->setEnabled(true);
308
                angleLabel->setEnabled(true);
1809 subik 309
                colorWheel->makeTetradic();
310
        }
311
        fillColorList();
312
        setPreview();
313
}
314
 
315
void ColorWheelDialog::colorWheel_clicked(int, const QPoint&)
316
{
317
        typeCombo_activated(typeCombo->currentItem());
318
}
319
 
320
void ColorWheelDialog::angleSpin_valueChanged(int value)
321
{
322
        colorWheel->angle = value;
323
        typeCombo_activated(typeCombo->currentItem());
324
}
325
 
326
void ColorWheelDialog::addButton_clicked()
327
{
328
        QString status("<qt><h2>" + tr("Merging colors") + "</h2><p>");
1811 subik 329
        bool err = false;
1809 subik 330
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
331
        {
5781 cbradney 332
                if (m_Doc->PageColors.contains(it.key()))
1811 subik 333
                {
3372 cbradney 334
                        status += "<b>" + tr("Error: ") + "</b>" + tr("Color %1 exists already!").arg(it.key()) + "<br/>";
1811 subik 335
                        err = true;
336
                }
1809 subik 337
                else
338
                {
3372 cbradney 339
                        status += tr("Color %1 appended.").arg(it.key()) + "<br/>";
5781 cbradney 340
                        m_Doc->PageColors[it.key()] = it.data();
1809 subik 341
                }
342
        }
1811 subik 343
        status += "<p>" + tr("Now opening the color manager.") + "</p></qt>";
344
        if (err)
345
        {
346
                QMessageBox::information(this, tr("Color Merging"), status);
5781 cbradney 347
                m_Doc->scMW()->slotEditColors();
1811 subik 348
                return;
349
        }
5781 cbradney 350
        m_Doc->scMW()->propertiesPalette->updateColorList();
351
        m_Doc->scMW()->propertiesPalette->updateCList();
1809 subik 352
        accept();
353
}
354
 
1811 subik 355
void ColorWheelDialog::replaceButton_clicked()
356
{
357
        for (ColorList::iterator it = colorWheel->colorList.begin(); it != colorWheel->colorList.end(); ++it)
358
        {
5781 cbradney 359
                m_Doc->PageColors[it.key()] = it.data();
1811 subik 360
        }
5781 cbradney 361
        m_Doc->scMW()->propertiesPalette->updateColorList();
362
        m_Doc->scMW()->propertiesPalette->updateCList();
1811 subik 363
        accept();
364
}
1809 subik 365
 
366
void ColorWheelDialog::cancelButton_clicked()
367
{
368
        reject();
369
}
370
 
2911 subik 371
void ColorWheelDialog::defectCombo_activated(int /*index*/)
372
{
373
        setPreview();
374
}
375
 
1809 subik 376
void ColorWheelDialog::setPreview()
377
{
378
        int x = previewLabel->width();
379
        int y = previewLabel->height();
2886 fschmid 380
        QValueList<ScColor> cols = colorWheel->colorList.values();
1809 subik 381
        int xstep = x / cols.count();
382
        QPixmap pm = QPixmap(x, y);
383
        QPainter *p = new QPainter(&pm);
384
        QFontMetrics fm = p->fontMetrics();
385
 
386
        pm.fill(Qt::white);
387
        p->setPen(Qt::white);
388
        p->drawRect(0, 0, x, y);
389
        for (uint i = 0; i < cols.count(); ++i)
390
        {
2911 subik 391
                QColor c = computeDefect(cols[i].getRGBColor());
392
                p->setPen(c);
393
                p->setBrush(c);
1809 subik 394
                p->drawRect(i * xstep, 0, xstep, y);
395
        }
396
        p->setPen(Qt::black);
397
        p->setBrush(Qt::black);
398
        p->drawText(15, 5 + fm.height(), "Lorem ipsum dolor sit amet");
399
        p->setPen(Qt::white);
400
        p->setBrush(Qt::white);
3374 subik 401
        p->drawText(125, y - 5 - fm.height(), "Lorem ipsum dolor sit amet");
1809 subik 402
        p->end();
403
        delete(p);
404
        previewLabel->clear();
405
        previewLabel->setPixmap(pm);
406
}
2911 subik 407
 
408
QColor ColorWheelDialog::computeDefect(QColor c)
409
{
3107 subik 410
        if (defectCombo->currentItem() == VisionDefectColor::normalVision)
2911 subik 411
                return c;
3107 subik 412
        VisionDefectColor *defect = new VisionDefectColor(c);
413
        defect->deficiency = defectCombo->currentItem();
414
        defect->convertDefect();
415
        QColor nc = defect->getColor();
416
        delete defect;
417
        return nc;
2911 subik 418
}
419
 
3331 subik 420
void ColorWheelDialog::createColor()
421
{
3710 subik 422
        ScColor beginColor;
423
        /* these 2 variables are defined for CMYKChoose constructor.
3331 subik 424
        No other need to use them. */
425
        ColorList tmpcl;
426
        QStringList tmpsl;
3710 subik 427
 
428
        beginColor.fromQColor(colorWheel->actualColor);
5915 fschmid 429
        CMYKChoose* dia = new CMYKChoose(this, m_Doc, beginColor, tr("New Color"), &tmpcl, tmpsl, true);
3331 subik 430
        if (dia->exec())
3353 subik 431
                userColorInput(dia->Farbe.getRGBColor());
3331 subik 432
        delete dia;
433
}
434
 
3709 subik 435
void ColorWheelDialog::setColorComponents()
436
{
3710 subik 437
        CwSetColor *dia = new CwSetColor(colorWheel->actualColor, this);
3709 subik 438
        if (dia->exec())
439
                userColorInput(dia->newColor);
440
        delete dia;
441
}
442
 
3331 subik 443
void ColorWheelDialog::importColor()
444
{
5781 cbradney 445
        ScribusColorList *dia = new ScribusColorList(m_Doc, this, "dia", true, 0);
3331 subik 446
        if (dia->exec())
3353 subik 447
                userColorInput(dia->selectedColor);
3331 subik 448
        delete dia;
449
}
3353 subik 450
 
451
void ColorWheelDialog::userColorInput(QColor c)
452
{
453
        if (colorWheel->recomputeColor(c))
454
                typeCombo_activated(typeCombo->currentItem());
455
        else
456
                QMessageBox::information(this, caption(),
457
                                "<qt>" + tr("Unable to find the requested color. "
3354 cbradney 458
                                                        "You have probably selected black, gray or white. "
459
                                                        "There is no way to process this color.") + "</qt>");
3353 subik 460
}