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 "PanelWidget.h"
8 #include "ui_PanelWidget.h"
9 
10 #include "GetPluginDialog.h"
11 #include "AppDialog.h"
12 #include "ScriptDialog.h"
13 
PanelWidget(QWidget * parent,QWidget * Main,LPlugins * Pinfo)14 PanelWidget::PanelWidget(QWidget *parent, QWidget *Main, LPlugins *Pinfo) : QWidget(parent), ui(new Ui::PanelWidget){
15   ui->setupUi(this);
16   mainui = Main;
17   PINFO = Pinfo;
18   //Fill up the menu's with the valid entries
19   ui->combo_align->addItem(tr("Top/Left"), "left");
20   ui->combo_align->addItem(tr("Center"), "center");
21   ui->combo_align->addItem(tr("Bottom/Right"), "right");
22   ui->combo_edge->addItem(tr("Top"), "top");
23   ui->combo_edge->addItem(tr("Bottom"), "bottom");
24   ui->combo_edge->addItem(tr("Left"), "left");
25   ui->combo_edge->addItem(tr("Right"), "right");
26 
27   LoadIcons();
28   //Now connect any other signals/slots
29   connect(ui->combo_edge, SIGNAL(currentIndexChanged(int)), this, SLOT(ItemChanged()) );
30   connect(ui->combo_align, SIGNAL(currentIndexChanged(int)), this, SLOT(ItemChanged()) );
31   connect(ui->spin_plength, SIGNAL(valueChanged(int)), this, SLOT(ItemChanged()) );
32   connect(ui->spin_pxthick, SIGNAL(valueChanged(int)), this, SLOT(ItemChanged()) );
33   connect(ui->check_autohide, SIGNAL(stateChanged(int)), this, SLOT(ItemChanged()) );
34   connect(ui->group_customcolor, SIGNAL(toggled(bool)), this, SLOT(ItemChanged()) );
35 
36 }
37 
~PanelWidget()38 PanelWidget::~PanelWidget(){
39 
40 }
41 
LoadSettings(QSettings * settings,int Dnum,int Pnum)42 void PanelWidget::LoadSettings(QSettings *settings, int Dnum, int Pnum){
43   pnum = Pnum; dnum = Dnum; //save these for later
44   ui->label->setText( QString(tr("Panel %1")).arg(QString::number(Pnum+1) ) );
45   QString screenID = QApplication::screens().at(Dnum)->name();
46   QString prefix = "panel_"+screenID+"."+QString::number(Pnum)+"/";
47   qDebug() << "Loading Panel Settings:" << prefix;
48   //Now load the settings into the GUI
49   int tmp = ui->combo_align->findData( settings->value(prefix+"pinLocation","center").toString().toLower() );
50   if(tmp>=0){ ui->combo_align->setCurrentIndex( tmp ); }
51   tmp = ui->combo_edge->findData( settings->value(prefix+"location","top").toString().toLower() );
52   if(tmp>=0){ ui->combo_edge->setCurrentIndex( tmp ); }
53   ui->spin_plength->setValue( qRound(settings->value( prefix+"lengthPercent",100).toDouble()) );
54   ui->spin_pxthick->setValue( qRound(settings->value( prefix+"height",30).toDouble()) );
55   ui->check_autohide->setChecked( settings->value(prefix+"hidepanel", false).toBool() );
56   ui->group_customcolor->setChecked( settings->value(prefix+"customColor",false).toBool() );
57   ui->label_color_sample->setWhatsThis( settings->value(prefix+"color","rgba(255,255,255,160)").toString());
58   ui->list_plugins->clear();
59   QStringList plugs = settings->value(prefix+"pluginlist",QStringList()).toStringList();
60   for(int i=0; i<plugs.length(); i++){
61     QString pid = plugs[i].section("---",0,0);
62       if(pid.startsWith("applauncher")){
63 	XDGDesktop desk(pid.section("::",1,1));
64 	if(desk.type!=XDGDesktop::BAD){ //still need to allow invalid apps
65 	  QListWidgetItem *it = new QListWidgetItem( LXDG::findIcon(desk.icon,""), desk.name );
66 	      it->setWhatsThis(plugs[i]); //make sure to preserve the entire plugin ID (is the unique version)
67 	  ui->list_plugins->addItem(it);
68 	}
69 
70       }else if(pid.startsWith("jsonmenu")){
71         LPI info = PINFO->panelPluginInfo( plugs[i].section("::::",0,0) );
72         if(info.ID.isEmpty()){ continue; } //invalid plugin type (no longer available?)
73         QString exec = plugs[i].section("::::",1,1);
74         QListWidgetItem *item = new QListWidgetItem();
75           item->setWhatsThis( plugs[i] );
76           item->setIcon( LXDG::findIcon(plugs[i].section("::::",3,3),info.icon) );
77           item->setText( plugs[i].section("::::",2,2) +" ("+info.name+")" );
78           item->setToolTip( info.description );
79         ui->list_plugins->addItem(item);
80 
81       }else{
82         LPI info = PINFO->panelPluginInfo(pid);
83         if(!info.ID.isEmpty()){
84           QListWidgetItem *it = new QListWidgetItem( LXDG::findIcon(info.icon,""), info.name );
85 	      it->setWhatsThis(plugs[i]); //make sure to preserve the entire plugin ID (is the unique version)
86 	  ui->list_plugins->addItem(it);
87         }
88       }
89   }
90   reloadColorSample();
91 }
92 
SaveSettings(QSettings * settings,QString screenID)93 void PanelWidget::SaveSettings(QSettings *settings, QString screenID){//save the current settings
94   if(screenID.isEmpty()){ screenID = QApplication::screens().at(dnum)->name(); }
95   QString prefix = "panel_"+screenID+"."+QString::number(pnum)+"/";
96   qDebug() << "Saving panel settings:" << prefix;
97   settings->setValue(prefix+"location", ui->combo_edge->currentData().toString() );
98   settings->setValue(prefix+"pinLocation", ui->combo_align->currentData().toString() );
99   settings->setValue(prefix+"lengthPercent", ui->spin_plength->value() );
100   settings->setValue(prefix+"height", ui->spin_pxthick->value() );
101   settings->setValue(prefix+"hidepanel", ui->check_autohide->isChecked() );
102   settings->setValue(prefix+"customColor", ui->group_customcolor->isChecked() );
103   settings->setValue(prefix+"color", ui->label_color_sample->whatsThis() );
104   QStringList plugs;
105   for(int i=0; i<ui->list_plugins->count(); i++){
106      plugs << ui->list_plugins->item(i)->whatsThis();
107   }
108   settings->setValue(prefix+"pluginlist", plugs );
109 
110 }
111 
PanelNumber()112 int PanelWidget::PanelNumber(){
113   return pnum;
114 }
115 
ChangePanelNumber(int newnum)116 void PanelWidget::ChangePanelNumber(int newnum){
117   ui->label->setText( QString(tr("Panel %1")).arg(QString::number(newnum+1) ) );
118   pnum = newnum; //So we can retain the current settings, but will save them with a different number
119 }
120 
LoadIcons()121 void PanelWidget::LoadIcons(){
122   ui->tool_rm->setIcon( LXDG::findIcon("list-remove","") );
123   ui->tool_remplugin->setIcon( LXDG::findIcon("list-remove","") );
124   ui->tool_addplugin->setIcon( LXDG::findIcon("list-add","") );
125   ui->tool_upplugin->setIcon( LXDG::findIcon("go-up","") );
126   ui->tool_downplugin->setIcon( LXDG::findIcon("go-down","") );
127   ui->tool_selectcolor->setIcon( LXDG::findIcon("preferences-desktop-color","") );
128   ui->tabWidget->setTabIcon(0,LXDG::findIcon("transform-move",""));
129   ui->tabWidget->setTabIcon(1,LXDG::findIcon("preferences-desktop-display",""));
130   ui->tabWidget->setTabIcon(2,LXDG::findIcon("preferences-plugin",""));
131 }
132 
reloadColorSample()133 void PanelWidget::reloadColorSample(){
134   ui->label_color_sample->setStyleSheet("background: "+ui->label_color_sample->whatsThis());
135 }
136 
getSysApp(bool allowreset)137 QString PanelWidget::getSysApp(bool allowreset){
138   AppDialog dlg(this);
139     dlg.allowReset(allowreset);
140     dlg.exec();
141   if(dlg.appreset && allowreset){
142     return "reset";
143   }else{
144     return dlg.appselected;
145   }
146 }
147 
getColorStyle(QString current,bool allowTransparency)148 QString PanelWidget::getColorStyle(QString current, bool allowTransparency){
149   QString out;
150   //Convert the current color string into a QColor
151   QStringList col = current.section(")",0,0).section("(",1,1).split(",");
152   if(col.length()!=4){ col.clear(); col << "255" << "255" << "255" << "255"; }
153   QColor ccol = QColor(col[0].toInt(), col[1].toInt(), col[2].toInt(), col[3].toInt()); //RGBA
154   QColor ncol;
155     if(allowTransparency){ ncol= QColorDialog::getColor(ccol, this, tr("Select Color"), QColorDialog::ShowAlphaChannel); }
156     else{ ncol= QColorDialog::getColor(ccol, this, tr("Select Color")); }
157   //Now convert the new color into a usable string and return
158   if(ncol.isValid()){ //if the dialog was not cancelled
159     if(allowTransparency){
160       out = "rgba("+QString::number(ncol.red())+","+QString::number(ncol.green())+","+QString::number(ncol.blue())+","+QString::number(ncol.alpha())+")";
161     }else{
162       out = "rgb("+QString::number(ncol.red())+","+QString::number(ncol.green())+","+QString::number(ncol.blue())+")";
163     }
164   }
165   return out;
166 }
167 
on_tool_rm_clicked()168 void PanelWidget::on_tool_rm_clicked(){
169   emit PanelRemoved(pnum);
170 }
171 
ItemChanged()172 void PanelWidget::ItemChanged(){
173   emit PanelChanged();
174 }
175 
UseColorChanged()176 void PanelWidget::UseColorChanged(){
177 
178   emit PanelChanged();
179 }
180 
on_tool_selectcolor_clicked()181 void PanelWidget::on_tool_selectcolor_clicked(){
182   QString color = getColorStyle(ui->label_color_sample->whatsThis());
183   if( color.isEmpty()){ return; }
184   ui->label_color_sample->setWhatsThis(color);
185   reloadColorSample();
186   emit PanelChanged();
187 }
188 
on_tool_addplugin_clicked()189 void PanelWidget::on_tool_addplugin_clicked(){
190   GetPluginDialog dlg(mainui);
191 	dlg.LoadPlugins("panel", PINFO);
192 	dlg.exec();
193   if(!dlg.selected){ return; } //cancelled
194   QString pan = dlg.plugID; //getNewPanelPlugin();
195   QListWidgetItem *it = 0;
196   if(pan == "applauncher"){
197     //Prompt for the application to add
198     QString app =getSysApp();
199     if(app.isEmpty()){ return; } //cancelled
200     pan.append("::"+app);
201     XDGDesktop desk(app);
202     it = new QListWidgetItem( LXDG::findIcon(desk.icon,""), desk.name);
203       it->setWhatsThis(pan);
204 
205   }else if(pan=="jsonmenu"){
206     //Need to prompt for the script file, name, and icon to use
207     //new ID format: "jsonmenu"::::<exec to run>::::<name>::::<icon>
208     ScriptDialog SD(this);
209     SD.exec();
210     if(!SD.isValid()){ return; }
211     LPI info = PINFO->panelPluginInfo(pan);
212     it = new QListWidgetItem( LXDG::findIcon(SD.icon(),"text-x-script"), SD.name()+" ("+info.ID+")" );
213     it->setWhatsThis(info.ID+"::::"+SD.command()+"::::"+SD.name()+"::::"+SD.icon());
214     it->setToolTip( info.description );
215   }else{
216     if(pan.isEmpty()){ return; } //nothing selected
217     //Add the new plugin to the list
218     LPI info = PINFO->panelPluginInfo(pan);
219     it = new QListWidgetItem( LXDG::findIcon(info.icon,""), info.name);
220       it->setWhatsThis(info.ID);
221   }
222   //Now add the new item to the list
223   if(it!=0){
224     ui->list_plugins->addItem(it);
225     ui->list_plugins->setCurrentItem(it);
226     ui->list_plugins->scrollToItem(it);
227   }
228   emit PanelChanged();
229 }
230 
on_tool_remplugin_clicked()231 void PanelWidget::on_tool_remplugin_clicked(){
232   if(ui->list_plugins->currentRow() < 0){ return; }
233   delete ui->list_plugins->takeItem( ui->list_plugins->currentRow() );
234   emit PanelChanged();
235 }
236 
on_tool_upplugin_clicked()237 void PanelWidget::on_tool_upplugin_clicked(){
238   int row = ui->list_plugins->currentRow();
239   if( row <= 0){ return; }
240   ui->list_plugins->insertItem(row-1, ui->list_plugins->takeItem(row));
241   ui->list_plugins->setCurrentRow(row-1);
242   emit PanelChanged();
243 }
244 
on_tool_downplugin_clicked()245 void PanelWidget::on_tool_downplugin_clicked(){
246   int row = ui->list_plugins->currentRow();
247   if( row < 0 || row >= (ui->list_plugins->count()-1) ){ return; }
248   ui->list_plugins->insertItem(row+1, ui->list_plugins->takeItem(row));
249   ui->list_plugins->setCurrentRow(row+1);
250   emit PanelChanged();
251 }
252