1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <dcore/debug.h>
23 #include <dcore/global.h>
25 #include <QScrollArea>
29 #include <dgui/imagebutton.h>
30 #include <dgui/iconloader.h>
32 #include <dcore/config.h>
36 struct Palettes::Private
38 QComboBox
*chooserPalette
;
39 QStackedWidget
*containerPalette
;
40 CellsColor
*defaultPalette
;
41 CellsColor
*qtColorPalette
;
42 CellsColor
*customColorPalette
;
43 CellsColor
*customGradientPalette
;
48 Palettes::Palettes(QWidget
*parent
) : QFrame(parent
), d(new Private
)
50 d
->numColorRecent
= 0;
51 QVBoxLayout
*layout
= new QVBoxLayout
;
53 layout
->setSpacing(0);
56 setFrameStyle ( QFrame::Box
| QFrame::Raised
);
64 DCONFIG
->beginGroup("ColorPalette");
65 DCONFIG
->setValue("LastPalette", d
->chooserPalette
->currentIndex());;
68 QDir
brushesDir(CONFIG_DIR
+"/palettes");
70 if ( ! brushesDir
.exists() )
72 brushesDir
.mkdir(brushesDir
.path() );
75 dDebug("palette") << brushesDir
.path();
77 for(int i
= 0; i
< d
->containerPalette
->count(); i
++)
79 CellsColor
*palette
= qobject_cast
<CellsColor
*>(d
->containerPalette
->widget(i
) );
82 if(!palette
->isReadOnly())
84 palette
->save( CONFIG_DIR
+"/palettes/"+ palette
->name() + ".dpl" );
94 void Palettes::setupForm()
96 d
->chooserPalette
= new QComboBox(this);
99 d
->containerPalette
= new QStackedWidget(this);
100 layout()->addWidget(d
->chooserPalette
);
101 layout()->addWidget(d
->containerPalette
);
104 d
->defaultPalette
= new CellsColor(d
->containerPalette
);
105 d
->defaultPalette
->setName( tr("Default Palette") );
106 d
->defaultPalette
->setReadOnly( true);
109 addPalette(d
->defaultPalette
);
113 d
->qtColorPalette
= new CellsColor(d
->containerPalette
);
114 d
->qtColorPalette
->setReadOnly( true );
115 d
->qtColorPalette
->setName( tr("Named Colors") );
116 addPalette(d
->qtColorPalette
);
120 //Custom Color Palette
121 d
->customColorPalette
= new CellsColor(d
->containerPalette
);
122 d
->customColorPalette
->setName( tr("Custom Color Palette"));
123 addPalette( d
->customColorPalette
);
125 //Custom Gradient Palette
126 d
->customGradientPalette
= new CellsColor(d
->containerPalette
);
127 d
->customGradientPalette
->setName( tr("Custom Gradient Palette"));
128 d
->customGradientPalette
->setType( CellsColor::Gradient
);
129 addPalette( d
->customGradientPalette
);
131 connect(d
->chooserPalette
, SIGNAL(activated ( int )), d
->containerPalette
, SLOT(setCurrentIndex ( int )));
133 DCONFIG
->beginGroup("ColorPalette");
134 int lastIndex
= DCONFIG
->value("LastPalette").toInt();
139 d
->chooserPalette
->setCurrentIndex(lastIndex
);
140 d
->containerPalette
->setCurrentIndex(lastIndex
);
143 readPalettes(__PREFIX__
+QString("/share/dash/palettes")); // Pre-installed
144 readPalettes(CONFIG_DIR
+"/palettes"); // Locals
147 void Palettes::readPalettes(const QString
&paletteDir
)
149 dDebug("palette") << "Reading palettes from: " << paletteDir
;
150 QDir
dir(paletteDir
);
154 QStringList files
= dir
.entryList ( QStringList() << "*.dpl" );
155 QStringList::ConstIterator it
= files
.begin();
157 while(it
!= files
.end())
159 readPaletteFile(dir
.path()+"/"+*it
);
165 void Palettes::readPaletteFile(const QString
&file
)
167 PaletteParser parser
;
171 QList
<QBrush
> brushes
= parser
.brushes();
172 QString name
= parser
.paletteName();
173 bool editable
= parser
.paletteIsEditable();
174 addPalette(name
,brushes
,editable
);
178 dError() << "Error while parse palette file: " << file
;
182 void Palettes::addPalette(const QString
& name
, const QList
<QBrush
> & brushes
, bool editable
)
184 if( name
== d
->customColorPalette
->name())
186 QList
<QBrush
>::ConstIterator it
= brushes
.begin();
188 while(it
!= brushes
.end())
190 d
->customColorPalette
->addItem( *it
);
194 else if(name
== d
->customGradientPalette
->name())
196 QList
<QBrush
>::ConstIterator it
= brushes
.begin();
198 while(it
!= brushes
.end())
200 d
->customGradientPalette
->addItem( *it
);
206 CellsColor
*palette
= new CellsColor(d
->containerPalette
);
207 QList
<QBrush
>::ConstIterator it
= brushes
.begin();
209 while(it
!= brushes
.end())
211 palette
->addItem( *it
);
214 palette
->setName(name
);
217 palette
->setReadOnly( !editable
);
221 void Palettes::addPalette(CellsColor
*palette
)
223 connect(palette
, SIGNAL(itemEntered(QTableWidgetItem
*)), this, SLOT(changeColor(QTableWidgetItem
*)));
224 connect(palette
, SIGNAL(itemPressed(QTableWidgetItem
*)), this, SLOT(changeColor(QTableWidgetItem
*)));
225 d
->chooserPalette
->addItem(palette
->name());
226 d
->containerPalette
->addWidget(palette
);
230 void Palettes::changeColor(QTableWidgetItem
* item
)
235 emit
brushSelected(item
->background());
239 void Palettes::fillDefaultColors()
243 //First column, first 6 rows, a gray scale
244 for ( i
= 0; i
<= 5; i
++ )
246 d
->defaultPalette
->addItem(QColor( i
* 51, i
* 51, i
* 51 ));
248 //First column, last 6 rows, basic colors
249 d
->defaultPalette
->addItem(QColor( 255, 0, 0 ));
250 d
->defaultPalette
->addItem(QColor( 0, 255, 0 ));
251 d
->defaultPalette
->addItem(QColor( 0, 0, 255 ));
252 d
->defaultPalette
->addItem(QColor( 255, 255, 0 ));
253 d
->defaultPalette
->addItem(QColor( 0, 255, 255 ));
254 d
->defaultPalette
->addItem(QColor( 255, 0, 255 ));
256 //Segment from column 1 to 6 and row 0 to 5
257 for ( i
= 0; i
<= 5; i
++ )
259 for ( j
= 1; j
<= 6; j
++ )
261 d
->defaultPalette
->addItem(QColor( 0, ( j
- 1 ) * 51, i
* 51 ));
265 //Segment from column 1 to 6 and row 6 to 11
266 for ( i
= 6; i
<= 11; i
++ )
268 for ( j
= 1; j
<= 6; j
++ )
270 d
->defaultPalette
->addItem(QColor( 153, ( j
- 1 ) * 51, ( i
- 6 ) * 51 ));
274 //Segment from column 7 to 12 and row 0 to 5
275 for ( i
= 0; i
<= 5; i
++ )
277 for ( j
= 7; j
<= 12; j
++ )
279 d
->defaultPalette
->addItem(QColor( 51, ( j
- 7 ) * 51, i
* 51 ));
283 //Segment from column 7 to 12 and row 6 to 11
284 for ( i
= 6; i
<= 11; i
++ )
286 for ( j
= 7; j
<= 12; j
++ )
288 d
->defaultPalette
->addItem(QColor( 204, ( j
- 7 ) * 51, ( i
- 6 ) * 51 ));
292 //Segment from column 13 to 18 and row 0 to 5
293 for ( i
= 0; i
<= 5; i
++ )
295 for ( j
= 13; j
<= 18; j
++ )
297 d
->defaultPalette
->addItem(QColor( 102, ( j
- 13 ) * 51, i
* 51 ));
301 //Segment from column 13 to 18 and row 6 to 11
302 for ( i
= 6; i
<= 11; i
++ )
304 for ( j
= 13; j
<= 18; j
++ )
306 d
->defaultPalette
->addItem(QColor( 255, ( j
- 13 ) * 51, ( i
- 6 ) * 51 ));
312 void Palettes::fillNamedColor()
314 QStringList strColors
= QColor::colorNames ();
315 QStringList::ConstIterator it
= strColors
.begin();
317 while(it
!= strColors
.end() )
319 d
->qtColorPalette
->addItem( QColor(*it
) );
323 d
->qtColorPalette
->addItem(QColor(0,0,0,0));
324 d
->qtColorPalette
->addItem(QColor(0,0,0,50));
327 void Palettes::addCurrentColor()
329 CellsColor
*palette
= qobject_cast
<CellsColor
*>(d
->containerPalette
->currentWidget());
333 if( palette
->isReadOnly()
334 || (d
->currentBrush
.gradient() && palette
->type() == CellsColor::Color
)
335 || d
->currentBrush
.color().isValid() && palette
->type() == CellsColor::Gradient
)
337 if( 15 <= d
->currentBrush
.style() && d
->currentBrush
.style() < 18 )
339 palette
= d
->customGradientPalette
;
340 d
->chooserPalette
->setCurrentIndex( d
->chooserPalette
->findText ( d
->customGradientPalette
->name()));
341 d
->containerPalette
->setCurrentWidget ( d
->customGradientPalette
);
345 palette
= d
->customColorPalette
;
346 d
->chooserPalette
->setCurrentIndex( d
->chooserPalette
->findText ( d
->customColorPalette
->name()));
347 d
->containerPalette
->setCurrentWidget ( d
->customColorPalette
);
350 palette
->addItem(d
->currentBrush
);
354 void Palettes::removeCurrentColor()
356 DGui::CellView
*palette
= qobject_cast
<DGui::CellView
*>(d
->containerPalette
->currentWidget());
359 if( d
->defaultPalette
!= palette
)
361 //TODO: implementar en DGui::CellView removeItem
366 void Palettes::setupButtons()
368 QGroupBox
*containerButtons
= new QGroupBox(this);
369 QBoxLayout
*bLayout
= new QBoxLayout(QBoxLayout::LeftToRight
);
370 bLayout
->setMargin(0);
371 bLayout
->setSpacing(0);
373 containerButtons
->setLayout(bLayout
);
375 DGui::ImageButton
*addItem
= new DGui::ImageButton( DGui::IconLoader::self()->load("list-add.svg" ) , 22);
376 connect( addItem
, SIGNAL( clicked() ), SLOT( addCurrentColor() ));
377 addItem
->setToolTip(tr( "Add Color" ));
378 bLayout
->addWidget(addItem
);
380 DGui::ImageButton
*removeColor
= new DGui::ImageButton( DGui::IconLoader::self()->load("list-remove.svg" ), 22); //FIXME: cambiar los iconos
382 connect( removeColor
, SIGNAL( clicked() ), SLOT( removeCurrentColor() ) );
383 removeColor
->setToolTip(tr( "Remove Color" ));
384 bLayout
->addWidget(removeColor
);
386 layout()->addWidget(containerButtons
);
389 void Palettes::setBrush(const QBrush
& b
)