1 //===========================================
2 // Lumina Desktop Source Code
3 // Copyright (c) 2016, Ken Moore
4 // Available under the 3-clause BSD license
5 // See the LICENSE file for full details
6 //===========================================
7 #include "page_interface_panels.h"
8 #include "ui_page_interface_panels.h"
9 #include <QInputDialog>
10
11 #include "../GetPluginDialog.h"
12 #include "../AppDialog.h"
13
14 //==========
15 // PUBLIC
16 //==========
page_interface_panels(QWidget * parent)17 page_interface_panels::page_interface_panels(QWidget *parent) : PageWidget(parent), ui(new Ui::page_interface_panels()){
18 ui->setupUi(this);
19 loading = false;
20 PINFO = new LPlugins();
21 settings = new QSettings("lumina-desktop","desktopsettings");
22 connect(ui->tool_panels_add, SIGNAL(clicked()), this, SLOT(newPanel()) );
23 updateIcons();
24
25 //Create panels container
26 QHBoxLayout *panels_layout = new QHBoxLayout();
27 panels_layout->setContentsMargins(0,0,0,0);
28 panels_layout->setAlignment(Qt::AlignLeft);
29 panels_layout->addStretch();
30 ui->scroll_panels->widget()->setLayout(panels_layout);
31 }
32
~page_interface_panels()33 page_interface_panels::~page_interface_panels(){
34 delete PINFO;
35 }
36
37 //================
38 // PUBLIC SLOTS
39 //================
SaveSettings()40 void page_interface_panels::SaveSettings(){
41 QString screenID = QApplication::screens().at(cscreen)->name();
42 QString DPrefix = "desktop-"+screenID+"/";
43 settings->setValue(DPrefix+"panels", PANELS.length());
44 for(int i=0; i<PANELS.length(); i++){
45 PANELS[i]->SaveSettings(settings);
46 }
47 //The plugin ID's will be changed to unique ID's by the desktop - reload in a moment
48 emit HasPendingChanges(false);
49 settings->sync(); //save to disk right now
50 QTimer::singleShot(1000, this, SLOT(LoadSettings()) );
51 }
52
LoadSettings(int screennum)53 void page_interface_panels::LoadSettings(int screennum){
54 if(screennum>=0){
55 cscreen = screennum;
56 }
57 loading = true;
58 emit HasPendingChanges(false);
59 emit ChangePageTitle( tr("Desktop Settings") );
60 QString screenID = QApplication::screens().at(cscreen)->name();
61 QString DPrefix = "desktop-"+screenID+"/";
62 int panelnumber = settings->value(DPrefix+"panels",-1).toInt();
63 if(panelnumber<0){ panelnumber = 0; }
64 QHBoxLayout *panels_layout = static_cast<QHBoxLayout*>(ui->scroll_panels->widget()->layout());
65
66 //Remove extra panels (if any)
67 for(int i=panelnumber; i<PANELS.length(); i++){
68 PanelWidget *tmp = PANELS.takeAt(i);
69 delete tmp;
70 i--;
71 }
72
73 int current_count = panels_layout->count()-1;
74
75 //Update current panels
76 for(int i=0; i<current_count; i++) {
77 PANELS[i]->LoadSettings(settings, cscreen, i);
78 }
79 //Create new panels
80 for(int i=current_count; i<panelnumber; i++){
81 PanelWidget *tmp = new PanelWidget(ui->scroll_panels->widget(), this, PINFO);
82 tmp->LoadSettings(settings, cscreen, i);
83 PANELS << tmp;
84 connect(tmp, SIGNAL(PanelChanged()), this, SLOT(panelValChanged()) );
85 connect(tmp, SIGNAL(PanelRemoved(int)), this, SLOT(removePanel(int)) );
86 panels_layout->insertWidget(panels_layout->count()-1, tmp);
87 }
88
89 QApplication::processEvents();
90 loading = false;
91 setupProfiles();
92 }
93
updateIcons()94 void page_interface_panels::updateIcons(){
95 ui->tool_panels_add->setIcon( LXDG::findIcon("list-add","") );
96 ui->tool_profile->setIcon( LXDG::findIcon("document-import","") );
97 }
98
99 //=================
100 // PRIVATE
101 //=================
setupProfiles()102 void page_interface_panels::setupProfiles(){
103 //qDebug() << "Start loading profiles";
104 if(ui->tool_profile->menu()==0){
105 ui->tool_profile->setMenu( new QMenu(this) );
106 connect(ui->tool_profile->menu(), SIGNAL(triggered(QAction*)), this, SLOT(applyProfile(QAction*)) );
107 }
108 else{ ui->tool_profile->menu()->clear(); }
109 ui->tool_profile->menu()->addSection("Profiles");
110 QAction *act = ui->tool_profile->menu()->addAction(tr("No Panels"));
111 act->setWhatsThis("none");
112 act = ui->tool_profile->menu()->addAction("Windows");
113 act->setWhatsThis("windows");
114 act = ui->tool_profile->menu()->addAction("GNOME2/MATE");
115 act->setWhatsThis("gnome2");
116 act = ui->tool_profile->menu()->addAction("XFCE");
117 act->setWhatsThis("xfce");
118 act = ui->tool_profile->menu()->addAction("Mac OSX");
119 act->setWhatsThis("osx");
120
121 //Add in any custom profiles
122 //qDebug() << " - read settings";
123 QStringList profilesAll = settings->childGroups().filter("panel_");
124 //qDebug() << " - get current screen";
125 QString current = QApplication::screens().at(cscreen)->name();
126 //qDebug() << " - filter list";
127 for(int i=0; i<profilesAll.length(); i++){
128 profilesAll[i] = profilesAll[i].section("_",1,-1).section(".",0,-2);
129 }
130 //qDebug() << "Found Profiles:" << profilesAll;
131 profilesAll.removeDuplicates();
132 profilesAll.removeAll(current);
133 QStringList profiles = profilesAll.filter("profile_");
134 for(int p=0; p<2; p++){
135 if(p==1){ profiles = profilesAll; } //use whats left of the total list
136 ui->tool_profile->menu()->addSection( p==0 ? tr("Custom Profiles") : tr("Copy Screen") );
137 for(int i=0; i<profiles.length(); i++){
138 if(p==0){ profilesAll.removeAll(profiles[i]); } //handling it now
139 QString title = profiles[i];
140 if(title.startsWith("profile_")){ title = title.section("profile_",-1); }
141 QMenu *tmp = new QMenu(ui->tool_profile->menu());
142 tmp->setTitle(title);
143 tmp->addAction(LXDG::findIcon("dialog-ok-apply",""), tr("Apply"))->setWhatsThis("profile_apply::::"+profiles[i]);
144 tmp->addAction(LXDG::findIcon("list-remove",""), tr("Delete"))->setWhatsThis("profile_remove::::"+profiles[i]);
145 ui->tool_profile->menu()->addMenu(tmp);
146 }
147 if(p==0){
148 //Now add the option to create a new profile
149 ui->tool_profile->menu()->addAction(LXDG::findIcon("list-add",""), tr("Create Profile"))->setWhatsThis("profile_new");
150 }
151 }
152 }
153
154 //=================
155 // PRIVATE SLOTS
156 //=================
panelValChanged()157 void page_interface_panels::panelValChanged(){
158 ui->tool_panels_add->setEnabled(PANELS.length() < 12);
159 if(!loading){ settingChanged(); }
160 }
161
newPanel()162 void page_interface_panels::newPanel(){
163 //if(panelnumber<0){ panelnumber=0; } //just in case
164 //panelnumber++;
165 //Now create a new Panel widget with this number
166 PanelWidget *tmp = new PanelWidget(ui->scroll_panels->widget(), this, PINFO);
167 tmp->LoadSettings(settings, cscreen, PANELS.length());
168 PANELS << tmp;
169 connect(tmp, SIGNAL(PanelChanged()), this, SLOT(panelValChanged()) );
170 connect(tmp, SIGNAL(PanelRemoved(int)), this, SLOT(removePanel(int)) );
171 static_cast<QBoxLayout*>(ui->scroll_panels->widget()->layout())->insertWidget(PANELS.length()-1, tmp);
172 //update the widget first (2 necessary for scroll below to work)
173 ui->scroll_panels->update();
174 QApplication::processEvents();
175 QApplication::processEvents();
176 ui->scroll_panels->ensureWidgetVisible(tmp);
177 panelValChanged();
178 }
179
removePanel(int pan)180 void page_interface_panels::removePanel(int pan){
181 //connected to a signal from the panel widget
182 bool changed = false;
183 for(int i=0; i<PANELS.length(); i++){
184 int num = PANELS[i]->PanelNumber();
185 if(num==pan){
186 delete PANELS.takeAt(i);
187 i--;
188 changed = true;
189 }else if(num > pan){
190 PANELS[i]->ChangePanelNumber(num-1);
191 changed = true;
192 }
193 }
194 if(!changed){ return; } //nothing done
195 panelValChanged();
196 }
197
applyProfile(QAction * act)198 void page_interface_panels::applyProfile(QAction *act){
199 QString wt = act->whatsThis();
200 if(wt.startsWith("profile_")){
201 //qDebug() << "Got Profile Action:" << wt;
202 if(wt=="profile_new"){
203 bool ok = false;
204 QString pname = QInputDialog::getText(this, tr("Create Profile"), tr("Name:"), QLineEdit::Normal, "", &ok,Qt::WindowFlags(), Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly );
205 if(!ok || pname.isEmpty()){ return; } //cancelled
206 pname = pname.replace(".","_").replace("/","_");
207 qDebug() << " - Make new profile:" << pname;
208 pname.prepend("profile_");
209 settings->setValue("desktop-"+pname+"/panels", PANELS.length());
210 for(int i=0; i<PANELS.length(); i++){
211 PANELS[i]->SaveSettings(settings, pname);
212 }
213 settings->sync(); //save to disk right now
214 setupProfiles();
215 }else if(wt.startsWith("profile_apply::::") ){
216 applyImport(wt.section("::::",-1) );
217 }else if(wt.startsWith("profile_remove::::") ){
218 QString pname = wt.section("::::",-1);
219 QStringList keys = settings->allKeys().filter(pname);
220 for(int i=0; i<keys.length(); i++){
221 if(keys[i].section("/",0,0).contains(pname)){ settings->remove(keys[i]); }
222 }
223 setupProfiles();
224 }
225 return;
226 }
227 //Manually saving settings based on built-in profile
228 QString screenID = QApplication::screens().at(cscreen)->name();
229 QString DPrefix = "desktop-"+screenID+"/";
230 QString PPrefix = "panel_"+screenID+"."; //NEED TO APPEND PANEL NUMBER (0+)
231 qDebug() << "Apply Profile:" << act->whatsThis() << "To Monitor:" << screenID;
232 if(act->whatsThis()=="none"){
233 settings->setValue(DPrefix+"panels", 0); //number of panels
234 }else if(act->whatsThis()=="windows"){
235 settings->setValue(DPrefix+"panels", 1); //number of panels
236 //Panel 1 settings (index 0)
237 settings->setValue(PPrefix+"0/customColor", false);
238 settings->setValue(PPrefix+"0/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.04)); //4% of screen height
239 settings->setValue(PPrefix+"0/hidepanel", false);
240 settings->setValue(PPrefix+"0/lengthPercent", 100);
241 settings->setValue(PPrefix+"0/location", "bottom");
242 settings->setValue(PPrefix+"0/pinLocation", "center");
243 settings->setValue(PPrefix+"0/pluginlist", QStringList() << "systemstart" << "taskmanager" << "spacer" << "systemtray" << "clock");
244 }else if(act->whatsThis()=="gnome2"){
245 settings->setValue(DPrefix+"panels", 2); //number of panels
246 //Panel 1 settings (index 0)
247 settings->setValue(PPrefix+"0/customColor", false);
248 settings->setValue(PPrefix+"0/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.02)); //2% of screen height
249 settings->setValue(PPrefix+"0/hidepanel", false);
250 settings->setValue(PPrefix+"0/lengthPercent", 100);
251 settings->setValue(PPrefix+"0/location", "top");
252 settings->setValue(PPrefix+"0/pinLocation", "center");
253 settings->setValue(PPrefix+"0/pluginlist", QStringList() << "appmenu" << "desktopbar" << "spacer" << "systemtray" << "clock" << "systemdashboard");
254 //Panel 2 settings (index 1)
255 settings->setValue(PPrefix+"1/customColor", false);
256 settings->setValue(PPrefix+"1/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.02)); //2% of screen height
257 settings->setValue(PPrefix+"1/hidepanel", false);
258 settings->setValue(PPrefix+"1/lengthPercent", 100);
259 settings->setValue(PPrefix+"1/location", "bottom");
260 settings->setValue(PPrefix+"1/pinLocation", "center");
261 settings->setValue(PPrefix+"1/pluginlist", QStringList() << "homebutton" << "taskmanager-nogroups" << "spacer" << "desktopswitcher");
262 }else if(act->whatsThis()=="xfce"){
263 settings->setValue(DPrefix+"panels", 2); //number of panels
264 //Panel 1 settings (index 0)
265 settings->setValue(PPrefix+"0/customColor", false);
266 settings->setValue(PPrefix+"0/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.02)); //2% of screen height
267 settings->setValue(PPrefix+"0/hidepanel", false);
268 settings->setValue(PPrefix+"0/lengthPercent", 100);
269 settings->setValue(PPrefix+"0/location", "top");
270 settings->setValue(PPrefix+"0/pinLocation", "center");
271 settings->setValue(PPrefix+"0/pluginlist", QStringList() << "appmenu" << "taskmanager-nogroups" << "spacer" << "desktopswitcher" << "clock" << "systemtray" << "systemdashboard");
272 //Panel 2 settings (index 1)
273 settings->setValue(PPrefix+"1/customColor", false);
274 settings->setValue(PPrefix+"1/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.04)); //4% of screen height
275 settings->setValue(PPrefix+"1/hidepanel", false);
276 settings->setValue(PPrefix+"1/lengthPercent", 20);
277 settings->setValue(PPrefix+"1/location", "bottom");
278 settings->setValue(PPrefix+"1/pinLocation", "center");
279 settings->setValue(PPrefix+"1/pluginlist", QStringList() << "applauncher::lumina-fm.desktop" << "line"<<"spacer" << "desktopbar" << "spacer" << "line" << "applauncher::lumina-search.desktop");
280 }else if(act->whatsThis()=="osx"){
281 settings->setValue(DPrefix+"panels", 2); //number of panels
282 //Panel 1 settings (index 0)
283 settings->setValue(PPrefix+"0/customColor", false);
284 settings->setValue(PPrefix+"0/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.02)); //2% of screen height
285 settings->setValue(PPrefix+"0/hidepanel", false);
286 settings->setValue(PPrefix+"0/lengthPercent", 100);
287 settings->setValue(PPrefix+"0/location", "top");
288 settings->setValue(PPrefix+"0/pinLocation", "center");
289 settings->setValue(PPrefix+"0/pluginlist", QStringList() << "systemdashboard" << "spacer" << "systemtray" << "clock" << "applauncher::lumina-search.desktop");
290 //Panel 2 settings (index 1)
291 settings->setValue(PPrefix+"1/customColor", false);
292 settings->setValue(PPrefix+"1/height", qRound(QApplication::screens().at(cscreen)->virtualSize().height()*0.04)); //4% of screen height
293 settings->setValue(PPrefix+"1/hidepanel", false);
294 settings->setValue(PPrefix+"1/lengthPercent", 80);
295 settings->setValue(PPrefix+"1/location", "bottom");
296 settings->setValue(PPrefix+"1/pinLocation", "center");
297 settings->setValue(PPrefix+"1/pluginlist", QStringList() << "systemstart" << "applauncher::lumina-fm.desktop" << "desktopbar"<<"spacer" << "line"<< "taskmanager");
298
299 }else{
300 qDebug() << " - unknown profile! ("+act->whatsThis()+")";
301 return;
302 }
303 //Now flush the settings to disk and reload the interface
304 settings->sync(); //save to disk right now
305 QTimer::singleShot(1000, this, SLOT(LoadSettings()) );
306 }
307
applyImport(QAction * act)308 void page_interface_panels::applyImport(QAction *act){
309 applyImport(act->whatsThis());
310 }
311
applyImport(QString fromID)312 void page_interface_panels::applyImport(QString fromID){
313 QString cID = QApplication::screens().at(cscreen)->name();
314 //QString DPrefix = "desktop-"+screenID+"/";
315 qDebug() << "Import Panels from " << fromID << " to " << cID;
316 //First find all the values associated with this ID
317 int pannum = settings->value("desktop-"+fromID+"/panels").toInt();
318 QStringList pans = settings->allKeys().filter("panel_"+fromID);
319 fromID.prepend("panel_");
320
321 //save the number of panels which is active
322 settings->setValue("desktop-"+cID+"/panels", pannum);
323 //Now move over all the panel settings associated with the fromID
324 cID.prepend("panel_");
325 for(int i=0; i<pans.length(); i++){
326 QString newvar = pans[i];
327 newvar.replace(fromID, cID);
328 settings->setValue(newvar, settings->value(pans[i]) );
329 }
330 //Now flush the settings to disk and reload the interface
331 settings->sync(); //save to disk right now
332 QTimer::singleShot(1000, this, SLOT(LoadSettings()) );
333 }
334