source: lumina/lumina-desktop/LDesktop.cpp @ 59fa950

enter/10releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 59fa950 was 59fa950, checked in by Ken Moore <ken@…>, 11 months ago

Now the desktop plugins look and function a lot better. I might need to add a "snap to grid" ability later though...

  • Property mode set to 100644
File size: 10.2 KB
Line 
1//===========================================
2//  Lumina-DE source code
3//  Copyright (c) 2012, Ken Moore
4//  Available under the 3-clause BSD license
5//  See the LICENSE file for full details
6//===========================================
7#include "LDesktop.h"
8
9LDesktop::LDesktop(int deskNum) : QObject(){
10       
11  DPREFIX = "desktop-"+QString::number(deskNum)+"/";
12  desktopnumber = deskNum;
13  desktop = new QDesktopWidget();
14  defaultdesktop = (deskNum== desktop->primaryScreen());
15  desktoplocked = true;
16  xoffset = 0;
17  for(int i=0; i<desktopnumber; i++){
18    xoffset += desktop->screenGeometry(i).width();
19  }
20  deskMenu = new QMenu(0);
21    connect(deskMenu, SIGNAL(triggered(QAction*)), this, SLOT(SystemApplication(QAction*)) );
22  appmenu = new AppMenu(0);
23  workspacelabel = new QLabel(0);
24    workspacelabel->setAlignment(Qt::AlignCenter);
25  wkspaceact = new QWidgetAction(0);
26    wkspaceact->setDefaultWidget(workspacelabel);
27  //Setup the internal variables
28  settings = new QSettings(QSettings::UserScope, "LuminaDE","desktopsettings", this);
29  //qDebug() << " - Desktop Settings File:" << settings->fileName();
30  if(!QFile::exists(settings->fileName())){ settings->setValue(DPREFIX+"background/filelist",QStringList()<<"default"); settings->sync(); }
31  bgtimer = new QTimer(this);
32    bgtimer->setSingleShot(true);
33    connect(bgtimer, SIGNAL(timeout()), this, SLOT(UpdateBackground()) );
34  watcher = new QFileSystemWatcher(this);
35    connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(SettingsChanged()) );
36    watcher->addPath(settings->fileName());
37 
38  bgWindow = new QWidget(0);
39        bgWindow->setObjectName("bgWindow");
40        bgWindow->setContextMenuPolicy(Qt::CustomContextMenu);
41        LX11::SetAsDesktop(bgWindow->winId());
42        bgWindow->setGeometry(xoffset,0,desktop->screenGeometry().width(), desktop->screenGeometry().height());
43        connect(bgWindow, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowMenu()) );
44  bgDesktop = new QMdiArea(bgWindow);
45        //Make sure the desktop area is transparent to show the background
46        bgDesktop->setBackground( QBrush(Qt::NoBrush) );
47 
48  //Start the update processes
49  QTimer::singleShot(1,this, SLOT(UpdateMenu()) );
50  QTimer::singleShot(1,this, SLOT(UpdateBackground()) );
51  QTimer::singleShot(1,this, SLOT(UpdateDesktop()) );
52  QTimer::singleShot(1,this, SLOT(UpdatePanels()) );
53
54}
55
56LDesktop::~LDesktop(){
57  delete deskMenu;
58  delete appmenu;
59  delete bgWindow;
60  delete workspacelabel;
61  delete wkspaceact;
62}
63
64void LDesktop::SystemTerminal(){ 
65  QString term = settings->value("default-terminal","xterm").toString();
66  QProcess::startDetached(term); 
67}
68
69void LDesktop::SystemFileManager(){
70  QProcess::startDetached("lumina-fm"); 
71}
72
73void LDesktop::SystemApplication(QAction* act){
74  if(!act->whatsThis().isEmpty() && act->parent()==deskMenu){
75    QProcess::startDetached("lumina-open \""+act->whatsThis()+"\"");
76  }
77}
78
79void LDesktop::CreateDesktopPluginContainer(LDPlugin *plug){
80  LDPluginContainer *win = new LDPluginContainer(plug, desktoplocked);
81  if(desktoplocked){ bgDesktop->addSubWindow(win, Qt::FramelessWindowHint); }
82  else{ bgDesktop->addSubWindow(win, Qt::CustomizeWindowHint); }
83  win->loadInitialPosition();
84  win->show();
85}
86
87// =====================
88//     PRIVATE SLOTS
89// =====================
90void LDesktop::SettingsChanged(){
91  settings->sync(); //make sure to catch external settings changes
92  QTimer::singleShot(1,this, SLOT(UpdateMenu()) );
93  QTimer::singleShot(1,this, SLOT(UpdateBackground()) );
94  QTimer::singleShot(1,this, SLOT(UpdateDesktop()) );
95  QTimer::singleShot(1,this, SLOT(UpdatePanels()) );
96}
97
98void LDesktop::UpdateMenu(bool fast){
99  //qDebug() << " - Update Menu:" << desktopnumber;
100  //Put a label at the top
101  int num = LX11::GetCurrentDesktop();
102  qDebug() << "Found desktop number:" << num;
103  if(num < 0){ workspacelabel->setText( "<b>"+tr("Lumina Desktop")+"</b>"); }
104  else{ workspacelabel->setText( "<b>"+QString(tr("Workspace %1")).arg(QString::number(num+1))+"</b>"); }
105  if(fast){ return; } //already done
106  deskMenu->clear(); //clear it for refresh
107  deskMenu->addAction(wkspaceact);
108  deskMenu->addSeparator();
109  //Now load the user's menu setup and fill the menu
110  QStringList items = settings->value("menu/itemlist", QStringList()<< "terminal" << "filemanager" <<"applications" << "line" << "settings" ).toStringList();
111  for(int i=0; i<items.length(); i++){
112    if(items[i]=="terminal"){ deskMenu->addAction(LXDG::findIcon("utilities-terminal",""), tr("Terminal"), this, SLOT(SystemTerminal()) ); }
113    else if(items[i]=="filemanager"){ deskMenu->addAction( LXDG::findIcon("system-file-manager",""), tr("Browse System"), this, SLOT(SystemFileManager()) ); }
114    else if(items[i]=="applications"){ deskMenu->addMenu( LSession::applicationMenu() ); }
115    else if(items[i]=="line"){ deskMenu->addSeparator(); }
116    else if(items[i]=="settings"){ deskMenu->addMenu( LSession::settingsMenu() ); }
117    else if(items[i].startsWith("app::::") && items[i].endsWith(".desktop")){
118      //Custom *.desktop application
119      QString file = items[i].section("::::",1,1).simplified();
120      bool ok = false;
121      XDGDesktop xdgf = LXDG::loadDesktopFile(file, ok);
122      if(ok){
123        deskMenu->addAction( LXDG::findIcon(xdgf.icon,""), xdgf.name)->setWhatsThis(file);
124        }else{
125          qDebug() << "Could not load application file:" << file;
126        }
127    }
128  }
129  //Now add the system quit options
130  deskMenu->addSeparator();
131  if(!desktoplocked){ deskMenu->addAction(LXDG::findIcon("document-encrypt",""),tr("Lock Desktop"), this, SLOT(ToggleDesktopLock()) ); }
132  else{ deskMenu->addAction(LXDG::findIcon("document-decrypt",""),tr("Unlock Desktop"), this, SLOT(ToggleDesktopLock()) ); }
133  deskMenu->addAction(LXDG::findIcon("system-log-out",""), tr("Log Out"), this, SLOT(SystemLogout()) );
134}
135
136void LDesktop::UpdateDesktop(){
137  qDebug() << " - Update Desktop:" << desktopnumber;
138  QStringList plugins = settings->value(DPREFIX+"pluginlist", QStringList()).toStringList();
139  if(defaultdesktop && plugins.isEmpty()){
140    plugins << "sample";
141  }
142  for(int i=0; i<plugins.length(); i++){
143    //See if this plugin is already there
144    LDPlugin *plug = 0;
145    for(int p=0; p<PLUGINS.length(); p++){
146      if(PLUGINS[p]->type()==plugins[i]){
147        plug = PLUGINS[p];
148        break;
149      }
150    }
151    if(plug==0){
152      //New Plugin
153      qDebug() << " -- New Plugin:" << plugins[i];
154      plug = NewDP::createPlugin(plugins[i], bgWindow, bgWindow->geometry());
155      if(plug != 0){ 
156        qDebug() << " -- Show Plugin";
157        PLUGINS << plug;
158        CreateDesktopPluginContainer(plug);
159      }
160    }
161 
162  }
163}
164
165void LDesktop::ToggleDesktopLock(){
166  desktoplocked = !desktoplocked; //flip to other value
167  //Remove all the current containers
168  QList<QMdiSubWindow*> wins = bgDesktop->subWindowList();
169  for(int i=0; i<wins.length(); i++){
170    bgDesktop->removeSubWindow(wins[i]->widget()); //unhook plugin from container
171    bgDesktop->removeSubWindow(wins[i]); //remove container from screen
172    delete wins[i]; //delete old container
173  }
174  //Now recreate all the containers on the screen
175  for(int i=0; i<PLUGINS.length(); i++){
176    CreateDesktopPluginContainer(PLUGINS[i]);
177  }
178  UpdateMenu(false); 
179}
180
181void LDesktop::UpdatePanels(){
182  qDebug() << " - Update Panels:" << desktopnumber;
183  int panels = settings->value(DPREFIX+"panels", -1).toInt();
184  if(panels==-1 && defaultdesktop){ panels=1; } //need at least 1 panel on the primary desktop
185  //Remove all extra panels
186  for(int i=0; i<PANELS.length(); i++){
187    if(panels <= PANELS[i]->number()){
188      delete PANELS.takeAt(i);
189      i--;
190    }
191  }
192  for(int i=0; i<panels; i++){
193    //Check for a panel with this number
194    bool found = false;
195    for(int p=0; p<PANELS.length(); p++){
196      if(PANELS[p]->number() == i){
197        found = true;
198        qDebug() << " -- Update panel "<< i;
199        //panel already exists - just update it
200        QTimer::singleShot(0, PANELS[i], SLOT(UpdatePanel()) );
201      }
202    }
203    if(!found){
204      qDebug() << " -- Create panel "<< i;
205      //New panel
206      PANELS << new LPanel(settings, desktopnumber, i);
207    }
208  }
209  //Give it a 1/2 second before ensuring that the visible desktop area is correct
210  QTimer::singleShot(500, this, SLOT(UpdateDesktopPluginArea()) );
211}
212
213void LDesktop::UpdateDesktopPluginArea(){
214  QRegion visReg( bgWindow->geometry() ); //visible region (not hidden behind a panel)
215  for(int i=0; i<PANELS.length(); i++){
216    visReg = visReg.subtracted( QRegion(PANELS[i]->geometry()) );
217  }
218  //Now make sure the desktop plugin area is only the visible area
219  bgDesktop->setGeometry( visReg.boundingRect() );
220  bgDesktop->setBackground( QBrush(Qt::NoBrush) );
221  bgDesktop->update();
222}
223 
224void LDesktop::UpdateBackground(){
225  //Get the current Background
226  qDebug() << " - Update Background:" << desktopnumber;
227  //Get the list of background(s) to show
228  QStringList bgL = settings->value(DPREFIX+"background/filelist", QStringList()).toStringList();
229  //qDebug() << " - List:" << bgL << CBG;
230    //Remove any invalid files
231    for(int i=0; i<bgL.length(); i++){ 
232      if( (!QFile::exists(bgL[i]) && bgL[i]!="default") || bgL[i].isEmpty()){ bgL.removeAt(i); i--; } 
233    }
234  //Determine which background to use next
235  int index = bgL.indexOf(CBG);
236  if( (index < 0) || (index >= bgL.length()-1) ){ index = 0; } //use the first file
237  else{ index++; } //use the next file in the list
238  QString bgFile;
239  if( bgL.isEmpty() && CBG.isEmpty()){ bgFile = "default"; }
240  else if( bgL.isEmpty() && QFile::exists(CBG) ){ bgFile = CBG; }
241  else if( bgL.isEmpty() ){ bgFile = "default"; }
242  else{ bgFile = bgL[index]; }
243  //Save this file as the current background
244  CBG = bgFile;
245  //qDebug() << " - Set Background to:" << CBG << index << bgL;
246  if( (bgFile.toLower()=="default")){ bgFile = "/usr/local/share/Lumina-DE/desktop-background.jpg"; }
247  //Now set this file as the current background
248  QString style = "QWidget#bgWindow{ border-image:url(%1) stretch;}";
249  style = style.arg(bgFile);
250  bgWindow->setStyleSheet(style);
251  bgWindow->show();
252  //Now reset the timer for the next change (if appropriate)
253  if(bgtimer->isActive()){ bgtimer->stop(); }
254  if(bgL.length() > 1){
255    //get the length of the timer (in minutes)
256    int min = settings->value(DPREFIX+"background/minutesToChange",5).toInt();
257    //restart the internal timer
258    if(min > 0){
259      bgtimer->start(min*60000); //convert from minutes to milliseconds
260    }
261  }
262}
Note: See TracBrowser for help on using the repository browser.