00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "ColoursDialog.hpp"
00024
00025 #include <QWidget>
00026 #include <QVBoxLayout>
00027 #include <QFrame>
00028 #include <QGridLayout>
00029 #include <QGroupBox>
00030 #include <QPushButton>
00031 #include <QFileDialog>
00032 #include <QDir>
00033 #include <QDataStream>
00034
00035 #include "PaletteDisplayer.hpp"
00036 #include "GradientDialog.hpp"
00037 #include "ColourDialog.hpp"
00038
00039 #ifndef QT_NO_DEBUG
00040 #include <iostream>
00041 #endif
00042
00043 ColoursDialog :: ColoursDialog(const QVector<QRgb> table, QWidget* parent)
00044 :QDialog(parent),
00045 pMainLayout(NULL), pButtonFrame(NULL), pButtonLayout(NULL),
00046 pd(NULL),
00047 pRandomButton(NULL), pGradientButton(NULL), pCustomiseButton(NULL), pValidateButton(NULL), pCancelButton(NULL),
00048 pLoadPaletteButton(NULL), pSavePaletteButton(NULL),
00049 oldTable(table), table(table)
00050 {
00051 pMainLayout = new QVBoxLayout(this);
00052
00053 pd = new PaletteDisplayer(table,this);
00054
00055 pMainLayout->addWidget(pd,0,Qt::AlignHCenter) ;
00056
00057 pButtonFrame = new QFrame(this);
00058 pButtonLayout = new QGridLayout(pButtonFrame);
00059 pMainLayout->addWidget(pButtonFrame,1,0);
00060
00061 pRandomButton = new QPushButton(tr("Randomise"), this);
00062 pGradientButton = new QPushButton(tr("Gradient"), this);
00063 pColoration1Button = new QPushButton(tr("Coloration1"), this);
00064 pColoration2Button = new QPushButton(tr("Coloration2"), this);
00065 pColoration3Button = new QPushButton(tr("Coloration3"), this);
00066 pCustomiseButton = new QPushButton(tr("Customise"), this);
00067
00068 pApplyButton = new QPushButton(tr("Apply"), this);
00069 pValidateButton = new QPushButton(tr("Ok"), this);
00070 pCancelButton = new QPushButton(tr("Cancel"), this);
00071
00072 pLoadPaletteButton = new QPushButton(tr("Load palette"), this);
00073 pSavePaletteButton = new QPushButton(tr("Save palette"), this);
00074
00075 pButtonLayout->addWidget(pRandomButton, 1, 0);
00076 pButtonLayout->addWidget(pGradientButton, 1, 1);
00077 pButtonLayout->addWidget(pColoration1Button, 1, 2);
00078 pButtonLayout->addWidget(pColoration2Button, 1, 3);
00079 pButtonLayout->addWidget(pColoration3Button, 1, 4);
00080 pButtonLayout->addWidget(pCustomiseButton, 1, 5);
00081 pButtonLayout->addWidget(pApplyButton, 2, 0);
00082 pButtonLayout->addWidget(pValidateButton, 2, 1);
00083 pButtonLayout->addWidget(pCancelButton, 2, 2);
00084 pButtonLayout->addWidget(pLoadPaletteButton, 2, 3);
00085 pButtonLayout->addWidget(pSavePaletteButton, 2, 4);
00086
00087 connect(pRandomButton, SIGNAL(clicked()), this, SLOT(randomColours()));
00088 connect(pGradientButton, SIGNAL(clicked()), this, SLOT(gradientColours()));
00089 connect(pColoration1Button, SIGNAL(clicked()), this, SLOT(coloration1()));
00090 connect(pColoration2Button, SIGNAL(clicked()), this, SLOT(coloration2()));
00091 connect(pColoration3Button, SIGNAL(clicked()), this, SLOT(coloration3()));
00092 connect(pCustomiseButton, SIGNAL(clicked()), this, SLOT(customiseTable()));
00093 connect(pApplyButton, SIGNAL(clicked()), this, SLOT(apply()));
00094 connect(pValidateButton, SIGNAL(clicked()), this, SLOT(validate()));
00095 connect(pCancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
00096 connect(pLoadPaletteButton, SIGNAL(clicked()), this, SLOT(loadPalette()));
00097 connect(pSavePaletteButton, SIGNAL(clicked()), this, SLOT(savePalette()));
00098
00099 #ifndef QT_NO_DEBUG
00100 std::cout << "ColoursDialog created" << std::endl;
00101 #endif
00102 }
00103
00104 ColoursDialog :: ~ColoursDialog(void)
00105 {
00106 delete pSavePaletteButton;
00107 delete pLoadPaletteButton;
00108 delete pCancelButton;
00109 delete pValidateButton;
00110 delete pApplyButton;
00111 delete pColoration1Button;
00112 delete pColoration2Button;
00113 delete pColoration3Button;
00114 delete pCustomiseButton;
00115 delete pGradientButton;
00116 delete pRandomButton;
00117
00118 delete pd;
00119
00120 delete pButtonLayout;
00121 delete pButtonFrame;
00122
00123 delete pMainLayout;
00124
00125 #ifndef QT_NO_DEBUG
00126 std::cout << "ColoursDialog deleted" << std::endl;
00127 #endif
00128 }
00129
00130 void ColoursDialog :: randomColours(void)
00131 {
00132 #ifndef QT_NO_DEBUG
00133 std::cout << "SLOT: ColoursDialog :: randomColours" << std::endl;
00134 #endif
00135
00136 for ( QVector<QRgb>::iterator it_colours = table.begin() ; it_colours != table.end() ; ++it_colours )
00137 {
00138 *it_colours = qRgb(qrand()%256, qrand()%256, qrand()%256);
00139 }
00140
00141 pd->setPalette(table);
00142 }
00143
00144 void ColoursDialog :: gradientColours(void)
00145 {
00146 GradientDialog gd(QColor(table[0]), QColor(table[255]),this);
00147
00148 if ( gd.exec() == 0 )
00149 {
00150 QColor startingColor = gd.getStartingColor();
00151 QColor endingColor = gd.getEndingColor();
00152
00153
00154 float rStep = (endingColor.red() - startingColor.red()) / 256.0f;
00155 float gStep = (endingColor.green() - startingColor.green()) / 256.0f;
00156 float bStep = (endingColor.blue() - startingColor.blue()) / 256.0f;
00157
00158
00159
00160 float rCounter = 0;
00161 float gCounter = 0;
00162 float bCounter = 0;
00163
00164 for ( QVector<QRgb>::iterator it_colours = table.begin() ; it_colours != table.end() ; ++it_colours )
00165 {
00166 *it_colours = qRgb(startingColor.red() + rCounter, startingColor.green() + gCounter, startingColor.blue() + bCounter);
00167
00168 rCounter += rStep;
00169 gCounter += gStep;
00170 bCounter += bStep;
00171 }
00172 }
00173
00174 pd->setPalette(table);
00175
00176 #ifndef QT_NO_DEBUG
00177 std::cout << "SLOT: ColoursDialog :: gradientColours" << std::endl;
00178 #endif
00179
00180 }
00181
00182 void ColoursDialog :: coloration1(void)
00183 {
00184 unsigned int counter = 1;
00185
00186
00187 for ( QVector<QRgb>::iterator it_colours = table.begin() ; it_colours != table.end() ; ++it_colours )
00188 {
00189 *it_colours = qRgb(256 - counter, 256 - counter * 2, 256 - counter * 4);
00190 counter ++;
00191 }
00192
00193 pd->setPalette(table);
00194
00195 #ifndef QT_NO_DEBUG
00196 std::cout << "SLOT: ColoursDialog :: coloration1" << std::endl;
00197 #endif
00198 }
00199
00200 void ColoursDialog :: coloration2(void)
00201 {
00202 unsigned int counter = 1;
00203
00204
00205 for ( QVector<QRgb>::iterator it_colours = table.begin() ; it_colours != table.end() ; ++it_colours )
00206 {
00207 *it_colours = qRgb(abs(128 - counter), (128 + counter) % 256, 255);
00208 counter ++;
00209 }
00210
00211 pd->setPalette(table);
00212
00213 #ifndef QT_NO_DEBUG
00214 std::cout << "SLOT: ColoursDialog :: coloration2" << std::endl;
00215 #endif
00216 }
00217
00218 void ColoursDialog :: coloration3(void)
00219 {
00220 unsigned int counter = 1;
00221
00222
00223 for ( QVector<QRgb>::iterator it_colours = table.begin() ; it_colours != table.end() ; ++it_colours )
00224 {
00225 *it_colours = qRgb(abs(256 - counter * 2), 0 + counter, abs(256 - counter * 8)%256);
00226 counter ++;
00227 }
00228
00229 pd->setPalette(table);
00230
00231 #ifndef QT_NO_DEBUG
00232 std::cout << "SLOT: ColoursDialog :: coloration3" << std::endl;
00233 #endif
00234 }
00235
00236 void ColoursDialog :: customiseTable(void)
00237 {
00238 ColourDialog cd(table, this);
00239
00240 connect(&cd, SIGNAL(needsApply(QVector<QRgb>)), this, SLOT(applyNewTable(QVector<QRgb>)));
00241
00242 if ( cd.exec() == 0 )
00243 {
00244 table = cd.getTable();
00245 pd->setPalette(table);
00246 }
00247 else
00248 {
00249
00250 pd->setPalette(table);
00251 }
00252
00253 disconnect(&cd, SIGNAL(needsApply(QVector<QRgb>)), this, SLOT(applyNewTable(QVector<QRgb>)));
00254
00255 #ifndef QT_NO_DEBUG
00256 std::cout << "SLOT: ColoursDialog :: customiseTable" << std::endl;
00257 #endif
00258 }
00259
00260 void ColoursDialog :: apply(void)
00261 {
00262 emit coloursTableChanged(table);
00263
00264 #ifndef QT_NO_DEBUG
00265 std::cout << "SLOT: ColoursDialog :: apply" << std::endl;
00266 #endif
00267 }
00268
00269 void ColoursDialog :: applyNewTable(QVector<QRgb> newTable)
00270 {
00271
00272
00273
00274
00275 emit coloursTableChanged(newTable);
00276
00277 #ifndef QT_NO_DEBUG
00278 std::cout << "SLOT: ColoursDialog :: applyNewTable" << std::endl;
00279 #endif
00280 }
00281
00282 void ColoursDialog :: validate(void)
00283 {
00284 this->done(0);
00285
00286 #ifndef QT_NO_DEBUG
00287 std::cout << "SLOT: ColoursDialog :: validate" << std::endl;
00288 #endif
00289 }
00290
00291 void ColoursDialog :: cancel(void)
00292 {
00293 table = oldTable;
00294 this->done(1);
00295
00296 #ifndef QT_NO_DEBUG
00297 std::cout << "SLOT: ColoursDialog :: cancel" << std::endl;
00298 #endif
00299 }
00300
00301 void ColoursDialog :: loadPalette(void)
00302 {
00303 QString filename = QFileDialog::getOpenFileName(this, tr("Open palette file"), QDir::currentPath(), "Palette (*.pal)");
00304
00305 if ( !filename.endsWith(QString(".pal")) )
00306 {
00307 filename = filename + QString(".pal");
00308 }
00309
00310 if ( !filename.isEmpty() )
00311 {
00312 QFile paletteFile(filename);
00313
00314 if ( paletteFile.open(QIODevice::ReadOnly) )
00315 {
00316 QVector<QRgb> table;
00317 QDataStream dataFile(&paletteFile);
00318
00319 while (!dataFile.atEnd())
00320 {
00321 QRgb color;
00322
00323 dataFile >> color;
00324 table.push_back(color);
00325 }
00326
00327 this->table = table;
00328 pd->setPalette(table);
00329
00330 paletteFile.close();
00331 }
00332 }
00333
00334 #ifndef QT_NO_DEBUG
00335 std::cout << "SLOT: ColoursDialog :: loadPalette" << std::endl;
00336 #endif
00337 }
00338
00339 void ColoursDialog :: savePalette(void)
00340 {
00341 QString filename = QFileDialog::getSaveFileName(this, tr("Save palette file"), QDir::currentPath(), "Palette (*.pal)");
00342
00343 if ( !filename.endsWith(QString(".pal")) )
00344 {
00345 filename = filename + QString(".pal");
00346 }
00347
00348 if ( !filename.isEmpty() )
00349 {
00350 QFile paletteFile(filename);
00351
00352 if ( paletteFile.open(QIODevice::WriteOnly) )
00353 {
00354 QDataStream dataFile(&paletteFile);
00355
00356
00357 for ( int i = 0 ; i < table.size() ; i++ )
00358 {
00359 dataFile << table[i];
00360 }
00361
00362 paletteFile.close();
00363 }
00364 }
00365
00366 #ifndef QT_NO_DEBUG
00367 std::cout << "SLOT: ColoursDialog :: savePalette" << std::endl;
00368 #endif
00369 }