1 #include "ThemeDialog.h"
2 #include "ui_ThemeDialog.h"
3 
4 #include <LUtils.h>
5 
ThemeDialog(QWidget * parent,LPlugins * plugs,QString themeFilePath)6 ThemeDialog::ThemeDialog(QWidget *parent, LPlugins *plugs, QString themeFilePath) : QDialog(parent), ui(new Ui::ThemeDialog){
7   ui->setupUi(this); //load the designer file
8   filepath = themeFilePath;
9   this->setWindowIcon( LXDG::findIcon("preferences-desktop-theme","") );
10   ui->line_name->setText( themeFilePath.section("/",-1).section(".qss",0,0) );
11   //Load the icons for the window
12   ui->push_cancel->setIcon( LXDG::findIcon("dialog-cancel","") );
13   ui->push_save->setIcon( LXDG::findIcon("document-save","") );
14   ui->push_apply->setIcon( LXDG::findIcon("dialog-ok","") );
15   ui->tool_color->setIcon( LXDG::findIcon("color-picker","") );
16   //Now create entries for the available colors in the database
17   QStringList colors = plugs->colorItems();
18   colors.sort();
19   colormenu = new QMenu(this);
20   for(int i=0; i<colors.length(); i++){
21     LPI info = plugs->colorInfo(colors[i]);
22     QAction *act = new QAction(info.name, this);
23 	act->setWhatsThis("%%"+info.ID+"%%");
24 	act->setToolTip(info.description);
25     colormenu->addAction(act);
26   }
27   ui->tool_color->setMenu(colormenu);
28   //Now load the given file
29   loadTheme();
30   connect(colormenu, SIGNAL(triggered(QAction*)),this, SLOT(menuTriggered(QAction*)) );
31   connect(ui->text_file, SIGNAL(textChanged()), this, SLOT(themeChanged()) );
32 
33   //Now center the window on the parent
34   QPoint cen = parent->geometry().center();
35   this->move( cen.x() - (this->width()/2) , cen.y() - (this->height()/2) );
36 }
37 
loadTheme()38 void ThemeDialog::loadTheme(){
39   QStringList contents = LUtils::readFile(filepath);
40   ui->text_file->setPlainText( contents.join("\n") );
41   ui->push_save->setEnabled(false);
42   ui->push_apply->setEnabled(false);
43 }
44 
saveTheme()45 void ThemeDialog::saveTheme(){
46   QString name = ui->line_name->text();
47   QStringList contents = ui->text_file->toPlainText().split("\n");
48   LTHEME::saveLocalTheme(name, contents);
49   ui->push_save->setEnabled(false);
50   ui->push_apply->setEnabled(false);
51 }
52 
themeChanged()53 void ThemeDialog::themeChanged(){
54   ui->push_save->setEnabled(true);
55   ui->push_apply->setEnabled(true);
56 }
57 
58 // BUTTONS
on_push_save_clicked()59 void ThemeDialog::on_push_save_clicked(){
60   //Now set the output values
61   themename = ui->line_name->text();
62   themepath = QDir::homePath()+"/.lumina/themes/"+themename+".qss.template";
63   //Check if that theme already exists
64   if(QFile::exists(themepath)){
65     if( QMessageBox::Yes != QMessageBox::question(this, tr("Theme Exists"), tr("This theme already exists.\n Overwrite it?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) ){
66       themename.clear();
67       themepath.clear();
68       return; //cancelled
69     }
70   }
71   //save the colors and close
72   saveTheme();
73   //this->close();
74 }
75 
on_push_apply_clicked()76 void ThemeDialog::on_push_apply_clicked(){
77   //Now set the output values
78   themename = ui->line_name->text();
79   themepath = QDir::homePath()+"/.lumina/themes/"+themename+".qss.template";
80   //Check if that theme already exists
81   if(QFile::exists(themepath)){
82     if( QMessageBox::Yes != QMessageBox::question(this, tr("Theme Exists"), tr("This theme already exists.\n Overwrite it?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) ){
83       themename.clear();
84       themepath.clear();
85       return; //cancelled
86     }
87   }
88   saveTheme();
89   this->close();
90 }
91 
on_push_cancel_clicked()92 void ThemeDialog::on_push_cancel_clicked(){
93   //Now clear the output values (just in case)
94   themename.clear();
95   themepath.clear();
96   this->close();
97 }
98 
menuTriggered(QAction * act)99 void ThemeDialog::menuTriggered(QAction *act){
100   ui->text_file->insertPlainText( act->whatsThis() );
101 }
102