1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2015, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #include "ColorDialog.h"
8 #include "ui_ColorDialog.h"
9 
10 #include <QColorDialog>
11 #include <QStringList>
12 
13 #include <LuminaXDG.h>
14 
ColorDialog(QSettings * set,QWidget * parent)15 ColorDialog::ColorDialog(QSettings *set, QWidget *parent) : QDialog(parent), ui(new Ui::ColorDialog()){
16   ui->setupUi(this);
17   settings = set;
18   connect(ui->push_cancel, SIGNAL(clicked()), this, SLOT(close()) );
19   connect(ui->push_apply, SIGNAL(clicked()), this, SLOT(saveColors()) );
20   connect(ui->push_getcolor, SIGNAL(clicked()), this, SLOT(changeColor()) );
21 }
22 
LoadColors()23 void ColorDialog::LoadColors(){
24   ui->treeWidget->clear();
25   QStringList colors = settings->allKeys().filter("colors/");
26 
27   for(int i=0; i<colors.length(); i++){
28     QTreeWidgetItem *it = new QTreeWidgetItem();
29     it->setText(0, colors[i].section("/",-1));
30     it->setText(1, settings->value(colors[i]).toString() );
31     it->setBackground(2, QBrush(QColor( it->text(1) ) ) );
32     ui->treeWidget->addTopLevelItem(it);
33   }
34 }
35 
updateIcons()36 void ColorDialog::updateIcons(){
37   this->setWindowIcon( LXDG::findIcon("format-fill-color") );
38   ui->push_cancel->setIcon( LXDG::findIcon("dialog-cancel") );
39   ui->push_apply->setIcon( LXDG::findIcon("dialog-ok") );
40   ui->push_getcolor->setIcon( LXDG::findIcon("format-fill-color") );
41 }
42 
saveColors()43 void ColorDialog::saveColors(){
44   for(int i=0; i<ui->treeWidget->topLevelItemCount(); i++){
45     QTreeWidgetItem *it = ui->treeWidget->topLevelItem(i);
46     settings->setValue("colors/"+it->text(0), it->text(1));
47   }
48   emit colorsChanged();
49   this->close();
50 }
51 
changeColor()52 void ColorDialog::changeColor(){
53   QTreeWidgetItem *it = ui->treeWidget->currentItem();
54   if(it==0){ return; }
55   QColor color = QColorDialog::getColor(QColor( it->text(1)), this, tr("Select Color"));
56   if(!color.isValid()){ return; }
57   it->setText(1, color.name());
58   it->setBackground(2, QBrush(color));
59 }
60