aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/libLumina/RootWindow.cpp
blob: ccda47e8e51baf02c56947d013736c498f4317f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2016, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "RootWindow.h"

#include <QDesktopWidget>
#include <QScreen>
#include <QDebug>

#define DEBUG 0

// === PUBLIC ===
RootWindow::RootWindow() : QWidget(0, Qt::Window | Qt::BypassWindowManagerHint | Qt::WindowStaysOnBottomHint){
  qRegisterMetaType<WId>("WId");
  autoResizeTimer = 0;
  lastActiveMouse = 0;
  mouseFocusTimer = 0;
  this->setMouseTracking(true);
}

RootWindow::~RootWindow(){

}

void RootWindow::start(){

  if(autoResizeTimer==0){
    autoResizeTimer = new QTimer(this);
      autoResizeTimer->setInterval(100); //1/10 second (collect all nearly-simultaneous signals and compress into a single update)
      autoResizeTimer->setSingleShot(true);
    connect(autoResizeTimer, SIGNAL(timeout()), this, SLOT(ResizeRoot()) );
    connect(QApplication::desktop(), SIGNAL(resized(int)), autoResizeTimer, SLOT(start()) );
    connect(QApplication::desktop(), SIGNAL(screenCountChanged(int)), autoResizeTimer, SLOT(start()) );
  }
  if(mouseFocusTimer==0){
    mouseFocusTimer = new QTimer(this);
    mouseFocusTimer->setInterval(100);
    connect(mouseFocusTimer, SIGNAL(timeout()), this, SLOT(checkMouseFocus()) );

  }
  this->show();
  ResizeRoot();
  emit RegisterVirtualRoot(this->winId());
}

// === PRIVATE ===
void RootWindow::updateScreenPixmap(screeninfo *info){
  QPixmap pix(info->area.size());
  if(info->scale == RootWindow::SolidColor){
    QColor color;
    if(info->file.startsWith("rgb(")){
      QStringList colors = info->file.section(")",0,0).section("(",1,1).split(",");
      color = QColor(colors[0].toInt(), colors[1].toInt(), colors[2].toInt());
    }else{
      color = QColor(info->file);
    }
    pix.fill(color);
  }else{
    QPixmap raw(info->file); //load the image from file
    //Now apply the proper aspect ratio as needed
    if(info->scale == RootWindow::Stretch || info->scale == RootWindow::Full || info->scale == RootWindow::Fit){
       Qt::AspectRatioMode armode = Qt::KeepAspectRatio;
       if(info->scale == RootWindow::Stretch ){  armode = Qt::IgnoreAspectRatio; }
      else if(info->scale == RootWindow::Full ){ armode = Qt::KeepAspectRatioByExpanding; }
      if(raw.height()!=info->area.height() && raw.width() !=info->area.width()){
        raw = raw.scaled(info->area.size(), armode);
      }
    }
    //Now calculate offset and draw width/height
    QRect drawRect(0,0, raw.width(), raw.height());
    if(info->scale == RootWindow::Full ){
      drawRect.moveTo( (info->area.width() - raw.width())/2, (info->area.height() - raw.height())/2 );
    }else if(info->scale == RootWindow::Fit ){
      drawRect.moveTo( (info->area.width() - raw.width())/2, (info->area.height() - raw.height())/2 );
    }else if(info->scale == RootWindow::Center ){
      drawRect.moveTo( (info->area.width() - raw.width())/2, (info->area.height() - raw.height())/2 );
    }else if(info->scale == RootWindow::Tile ){
      //Draw the entire area - no offset
      drawRect.setHeight(info->area.height());
      drawRect.setWidth(info->area.width());
    }else if(info->scale == RootWindow::BottomLeft ){
      drawRect.moveTo( 0 , info->area.height() - raw.height() );
    }else if(info->scale == RootWindow::BottomRight ){
      drawRect.moveTo( (info->area.width() - raw.width()), (info->area.height() - raw.height()) );
    }else if(info->scale == RootWindow::BottomCenter ){
      drawRect.moveTo( (info->area.width() - raw.width())/2, info->area.height() - raw.height() );
    }else if(info->scale == RootWindow::TopLeft ){
      drawRect.moveTo( 0, 0 );
    }else if(info->scale == RootWindow::TopRight ){
      drawRect.moveTo( (info->area.width() - raw.width()), 0);
    }else if(info->scale == RootWindow::TopCenter ){
      drawRect.moveTo( (info->area.width() - raw.width())/2, 0);
    }else if(info->scale == RootWindow::CenterLeft ){
      drawRect.moveTo( 0, (info->area.height() - raw.height())/2 );
    }else if(info->scale == RootWindow::CenterRight ){
      drawRect.moveTo( (info->area.width() - raw.width()), (info->area.height() - raw.height())/2 );
    }

  QPainter P(&pix);
    P.setBrush(raw);
    P.setBrushOrigin( drawRect.x(), drawRect.y() );
    P.drawRect( drawRect );
} //end SolidColor Check

  info->wallpaper = pix;
}

RootSubWindow* RootWindow::windowForId(WId id){
  RootSubWindow *tmp = 0;
  for(int i=0; i<WINDOWS.length() && tmp==0; i++){
    if(WINDOWS[i]->id() == id){ tmp = WINDOWS[i]; }
  }
  return tmp;
}

QScreen* RootWindow::screenUnderMouse(){
  QPoint mpos = QCursor::pos();
  QList<QScreen*> scrns = QApplication::screens();
  for(int i=0; i<scrns.length(); i++){
    if(scrns[i]->geometry().contains(mpos)){ return scrns[i]; }
  }
  //Could not find an exact match - just return the first one
  return scrns.first();
}

// === PUBLIC SLOTS ===
void RootWindow::ResizeRoot(){
  if(DEBUG){ qDebug() << "Resize Root..."; }
  QList<QScreen*> scrns = QApplication::screens();
  //Update all the screen locations and ID's in the WALLPAPERS list
  QRect fullscreen;
  QStringList valid;
  //Update the size of the rootWindow itself
  for(int i=0; i<scrns.length(); i++){
    if(DEBUG){ qDebug() << " - Found Screen:" << scrns[i]->name() << scrns[i]->geometry(); }
    fullscreen = fullscreen.united(scrns[i]->geometry());
    valid << scrns[i]->name();
    for(int j=0; j<WALLPAPERS.length(); j++){
      if(WALLPAPERS[j].id == scrns[i]->name()){
        QSize oldsize = WALLPAPERS[j].area.size();
        WALLPAPERS[j].area = scrns[i]->geometry();
        if(oldsize != WALLPAPERS[j].area.size()){ updateScreenPixmap(&WALLPAPERS[j]); }
        break;
      }
    }
  }
  //Now clean up any invalid screen info in the WALLPAPERS List
  QStringList invalid;
  for(int i=0; i<WALLPAPERS.length(); i++){
    if(valid.contains(WALLPAPERS[i].id)){
      valid.removeAll(WALLPAPERS[i].id); //Already handled - remove it from the list
    }else{
      invalid << WALLPAPERS[i].id;
      WALLPAPERS.takeAt(i);
      i--;
    }
  }
  //Trigger a repaint and send out any signals
  if(DEBUG){ qDebug() << " - FullScreen Geometry:" << fullscreen; }
  this->setGeometry(fullscreen);
  this->update();
  emit RootResized(fullscreen);
  if(!valid.isEmpty()){ emit NewScreens(valid); }
  if(!invalid.isEmpty()){ emit RemovedScreens(invalid); }
  if(DEBUG){ qDebug() << " - Geom after change:" << this->geometry(); }
}

void RootWindow::ChangeWallpaper(QString id, RootWindow::ScaleType scale, QString file){
  bool found = false;
  for(int i=0; i<WALLPAPERS.length() && !found; i++){
    if(WALLPAPERS[i].id == id){
      WALLPAPERS[i].scale = scale;
      WALLPAPERS[i].file = file;
      updateScreenPixmap(&WALLPAPERS[i]);
      //qDebug() << "   --- Updated Wallpaper:" << WALLPAPERS[i].id << WALLPAPERS[i].file << WALLPAPERS[i].area;
      found = true;
    }
  }
  if(!found){
    ResizeRoot();
    //Need to create a new screeninfo structure
    QList<QScreen*> scrns = QApplication::screens();
    for(int i=0; i<scrns.length(); i++){
      if(scrns[i]->name()==id){
        screeninfo info;
          info.id = id;
	  info.file = file;
          info.scale = scale;
          info.area = scrns[i]->geometry();
         updateScreenPixmap(&info);
         //qDebug() << "   --- Loaded Wallpaper:" << info.id << info.file << info.area;
        WALLPAPERS << info;
        break;
      }
    }
  } //end check for a new id

}

void RootWindow::checkMouseFocus(){
  QWidget *child = this->childAt(QCursor::pos());
  while(child!=0 && child->whatsThis()!="RootSubWindow"){
    child = child->parentWidget();
    if(child==this){ child = 0;} //end of the line
  }
  if(child==lastActiveMouse){ return; } //nothing new to do
  //Make sure the child is actually a RootSubWindow
  if(lastActiveMouse!=0){ lastActiveMouse->removeMouseFocus(); lastActiveMouse = 0; }
  if(child!=0){
    lastActiveMouse = static_cast<RootSubWindow*>(child);
    lastActiveMouse->giveMouseFocus();
  }
}

void RootWindow::NewWindow(NativeWindow *win){
  RootSubWindow *subwin = 0;
  //qDebug() << "Got New Window:" << win->property(NativeWindow::Title);
  for(int i=0; i<WINDOWS.length() && subwin==0; i++){
    if(WINDOWS[i]->id() == win->id()){ subwin = WINDOWS[i]; }
  }
  if(subwin==0){
    subwin = new RootSubWindow(this, win);
    subwin->setWhatsThis("RootSubWindow");
    connect(win, SIGNAL(WindowClosed(WId)), this, SLOT(CloseWindow(WId)) );
    WINDOWS << subwin;
  }
  CheckWindowPosition(win->id(), true); //first-time run
  //win->setProperty(NativeWindow::Visible, true);
  //win->requestProperty( NativeWindow::Active, true);
  win->requestProperties(QList<NativeWindow::Property>() << NativeWindow::Visible << NativeWindow::Active, QList<QVariant>() << true << true);
  if(!mouseFocusTimer->isActive()){ mouseFocusTimer->start(); }
}

void RootWindow::CloseWindow(WId win){
  for(int i=0; i<WINDOWS.length(); i++){
    if(WINDOWS[i]->id() == win){
      if(lastActiveMouse==WINDOWS[i]){ lastActiveMouse = 0; } //no longer valid
      WINDOWS.takeAt(i)->clientClosed();
      break;
    }
  }
  if(WINDOWS.isEmpty()){ mouseFocusTimer->stop(); } //no windows to look for
}

// === PRIVATE SLOTS ===

// === PROTECTED ===
void RootWindow::paintEvent(QPaintEvent *ev){
  //qDebug() << "RootWindow: PaintEvent:" << ev->rect();  //<< QDateTime::currentDateTime()->toString(QDateTime::ShortDate);
  //QWidget::paintEvent(ev);
  bool found = false;
  QPainter painter(this);
  QRect geom = ev->rect();
    geom.adjust(-100,-100,100,100); //give it a few more pixels in each direction to repaint (noticing some issues in Qt 5.7.1)
  for(int i=0; i<WALLPAPERS.length(); i++){
    if(WALLPAPERS[i].area.intersects(geom) ){
      found = true;
      QRect intersect = WALLPAPERS[i].area.intersected(geom);
      painter.drawPixmap( intersect, WALLPAPERS[i].wallpaper, intersect.translated(-WALLPAPERS[i].area.x(), -WALLPAPERS[i].area.y()) );
    }
  }
  painter.end();
  /*if(!found){
    QWidget::paintEvent(ev);
  }*/
}
bgstack15