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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
//===========================================
// Lumina-DE source code
// Copyright (c) 2015, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#ifndef _LUMINA_DESKTOP_LDESKTOP_PLUGIN_SPACE_H
#define _LUMINA_DESKTOP_LDESKTOP_PLUGIN_SPACE_H
#include <QListWidget>
#include <QDropEvent>
#include <QDrag> //includes all the QDrag*Event classes
#include <QUrl>
#include <QMimeData>
#include <QSettings>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include "desktop-plugins/LDPlugin.h"
#define MIMETYPE QString("x-special/lumina-desktop-plugin")
class LDesktopPluginSpace : public QWidget{
Q_OBJECT
signals:
void PluginRemovedByUser(QString ID);
void IncreaseIcons(); //increase default icon sizes
void DecreaseIcons(); //decrease default icon sizes
void HideDesktopMenu();
public:
LDesktopPluginSpace(QWidget *parent = 0);
~LDesktopPluginSpace();
void LoadItems(QStringList plugs, QStringList files);
//void setShowGrid(bool show); This is already implemented in QTableView (inherited)
void SetIconSize(int size);
void ArrangeTopToBottom(bool ttb); //if false, will arrange left->right
void cleanup();
public slots:
void UpdateGeom(int oldgrid = -1);
private:
QSettings *plugsettings;
QStringList plugins, deskitems;
QList<LDPlugin*> ITEMS;
bool TopToBottom;
float GRIDSIZE;
int RoundUp(double num){
int out = num; //This will truncate the number
if(out < num){ out++; } //need to increase by 1
return out;
}
void addDesktopItem(QString filepath); //This will convert it into a valid Plugin ID automatically
void addDesktopPlugin(QString plugID);
QRect findOpenSpot(int gridwidth = 1, int gridheight = 1, int startRow = 0, int startCol = 0, bool reversed = false, QString plugID = "");
QRect findOpenSpot(QRect grid, QString plugID, bool recursive = false);
QPoint posToGrid(QPoint pos){
//This assumes a point in widget-relative coordinates
pos.setX( RoundUp(pos.x()/GRIDSIZE));
pos.setY( RoundUp(pos.y()/GRIDSIZE));
return pos;
}
QRect geomToGrid(QRect geom, int grid = -1){
if(grid<0){
//use the current grid size
return QRect( RoundUp(geom.x()/GRIDSIZE), RoundUp(geom.y()/GRIDSIZE), \
RoundUp(geom.width()/GRIDSIZE), RoundUp(geom.height()/GRIDSIZE) );
}else{
//use the input grid size
return QRect( RoundUp(geom.x()/((double) grid)), RoundUp(geom.y()/((double) grid)), \
RoundUp(geom.width()/((double) grid)), RoundUp(geom.height()/((double) grid)) );
}
}
QRect gridToGeom(QRect grid){
//This function incorporates the bottom/right edge matchins procedures (for incomplete last grid)
QRect geom(grid.x()*GRIDSIZE, grid.y()*GRIDSIZE, grid.width()*GRIDSIZE, grid.height()*GRIDSIZE);
//Now check the edge conditions (last right/bottom grid points might be smaller than GRIDSIZE)
QSize areaSize = this->size(); //use the size of the area instead of the geometry - because we need this in child coordinates like "geom" above
//qDebug() << "GridToGeom:" << grid << geom << "Area size:" << areaSize;
if(geom.right() > areaSize.width() && (geom.right()-areaSize.width())<GRIDSIZE ){
geom.setRight(areaSize.width()-1); //match up with the edge
}
if(geom.bottom() > areaSize.height() && (geom.bottom() -areaSize.height())<GRIDSIZE ){
geom.setBottom(areaSize.height()-1); //match up with the edge
}
//qDebug() << " - Adjusted:" << geom;
return geom;
}
//Internal simplification for setting up a drag event
void setupDrag(QString id, QString type){
QMimeData *mime = new QMimeData;
mime->setData(MIMETYPE, QString(type+"::::"+id).toLocal8Bit() );
//If this is a desktop file - also add it to the generic URI list mimetype
if(id.startsWith("applauncher::")){
QList<QUrl> urilist;
urilist << QUrl::fromLocalFile( id.section("---",0,0).section("::",1,50) );
mime->setUrls(urilist);
}
//Create the drag structure
QDrag *drag = new QDrag(this);
drag->setMimeData(mime);
drag->exec(Qt::CopyAction);
}
bool ValidGrid(QRect grid){
//qDebug() << "Check Valid Grid:" << grid << RoundUp(this->width()/GRIDSIZE) << RoundUp(this->height()/GRIDSIZE);
//This just checks that the grid coordinates are not out of bounds - should still run ValidGeometry() below with the actual pixel geom
if(grid.x()<0 || grid.y()<0 || grid.width()<0 || grid.height()<0){ return false; }
else if( (grid.x()+grid.width()) > RoundUp(this->width()/GRIDSIZE) ){ return false; }
else if( (grid.y()+grid.height()) > RoundUp(this->height()/GRIDSIZE) ){ return false; }
return true;
}
bool ValidGeometry(QString id, QRect geom){
//First check that it is within the desktop area completely
// Note that "this->geometry()" is not in the same coordinate space as the geometry inputs
if(!QRect(0,0,this->width(), this->height()).contains(geom)){ return false; }
//Now check that it does not collide with any other items
for(int i=0; i<ITEMS.length(); i++){
if(ITEMS[i]->whatsThis()==id){ continue; }
else if(geom.intersects(ITEMS[i]->geometry())){ return false; }
}
return true;
}
LDPlugin* ItemFromID(QString ID){
for(int i=0; i<ITEMS.length(); i++){
if(ITEMS[i]->whatsThis()==ID){ return ITEMS[i]; }
}
return 0;
}
void MovePlugin(LDPlugin* plug, QRect geom){
plug->setGeometry( geom );
plug->setFixedSize(geom.size()); //needed for some plugins
plug->savePluginGeometry(geom);
}
private slots:
void reloadPlugins(bool ForceIconUpdate = false);
void StartItemMove(QString ID){
setupDrag(ID, "move");
}
void StartItemResize(QString ID){
setupDrag(ID, "resize");
}
void RemoveItem(QString ID){
//Special case - desktop file/dir link using the "applauncher" plugin
if(ID.startsWith("applauncher::")){
QFileInfo info(ID.section("---",0,0).section("::",1,50) );
if(info.exists() && info.absolutePath()==QDir::homePath()+"/Desktop"){
qDebug() << "Deleting Desktop Item:" << info.absoluteFilePath();
if(!info.isSymLink() && info.isDir()){ QProcess::startDetached("rm -r \""+info.absoluteFilePath()+"\""); }
else{ QFile::remove(info.absoluteFilePath()); } //just remove the file/symlink directly
emit PluginRemovedByUser(ID);
return;
}
}
//Any other type of plugin
for(int i=0; i<ITEMS.length(); i++){
if(ITEMS[i]->whatsThis()==ID){
ITEMS[i]->Cleanup();
delete ITEMS.takeAt(i);
break;
}
}
emit PluginRemovedByUser(ID);
}
protected:
//Need Drag and Drop functionality (internal movement)
void dragEnterEvent(QDragEnterEvent *ev){
if(ev->mimeData()->hasFormat(MIMETYPE) ){
ev->acceptProposedAction(); //allow this to be dropped here
}else if(ev->mimeData()->hasUrls()){
ev->acceptProposedAction(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dragMoveEvent(QDragMoveEvent *ev){
if(ev->mimeData()->hasFormat(MIMETYPE) ){
//Internal move/resize - Check for validity
QString act = QString( ev->mimeData()->data(MIMETYPE) );
LDPlugin *item = ItemFromID(act.section("::::",1,50));
//qDebug() << "Internal Move Event:" << act << ev->pos();
if(item!=0){
QRect geom = item->geometry();
QPoint grid = posToGrid(ev->pos());
if(act.section("::::",0,0)=="move"){
QPoint diff = grid - posToGrid(geom.center()); //difference in grid coords
//qDebug() << "Move Event:" << "Diff:" << diff << "Geom:" << geom << grid << ev->pos();
geom = geomToGrid(geom); //convert to grid coords
//qDebug() << "Move Event:" << "Old geom (grid):" << geom;
geom.moveTo( (geom.topLeft()+diff) );
//qDebug() << " - After Move:" << geom;
bool valid = ValidGrid(geom);
if(valid){
//Convert to pixel coordinates and check validity again
geom = gridToGeom(geom); //convert back to px coords with edge matching
valid = ValidGeometry(act.section("::::",1,50), geom);
}
if(valid){
MovePlugin(item, geom);
//item->setGeometry(geom);
//item->setFixedSize(geom.size()); //needed due to resizing limitations and such for some plugins
ev->acceptProposedAction();
//item->savePluginGeometry(geom); //save in pixel coords
}else{ ev->ignore(); } //invalid location
}else{
//Resize operation
QPoint diff = ev->pos() - (geom.center()-QPoint(1,1)); //need difference from center (pixels)
//Note: Use the 1x1 pixel offset to ensure that the center point is not exactly on a grid point intersection (2x2, 4x4, etc)
//qDebug() << "Resize Plugin:" << geom << grid << posToGrid(geom.center()) << diff;
geom = geomToGrid(geom); //convert to grid coordinates now
//qDebug() << " - Grid Geom:" << geom;
if(diff.x()<0){ geom.setLeft(ev->pos().x()/GRIDSIZE); } //expanding to the left (round down)
else if(diff.x()>0){ geom.setRight( ev->pos().x()/GRIDSIZE); } //expanding to the right (round down)
if(diff.y()<0){ geom.setTop( ev->pos().y()/GRIDSIZE); } //expanding above (round down)
else if(diff.y()>0){ geom.setBottom( ev->pos().y()/GRIDSIZE); } //expanding below (round down)
//qDebug() << " - Adjusted:" << geom;
bool valid = ValidGrid(geom);
if(valid){
//Convert to pixel coordinates and check validity again
geom = gridToGeom(geom); //convert back to px coords with edge matching
valid = ValidGeometry(act.section("::::",1,50), geom);
}
if(valid){
MovePlugin(item, geom);
//item->setGeometry(geom);
//item->setFixedSize(geom.size()); //needed due to resizing limitations and such for some plugins
ev->acceptProposedAction();
//item->savePluginGeometry(geom); //save in pixel coords
}else{ ev->ignore(); } //invalid location
}
}
}else if(ev->mimeData()->hasUrls()){
ev->acceptProposedAction(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dropEvent(QDropEvent *ev){
//QPoint grid = posToGrid(ev->pos());
if(ev->mimeData()->hasFormat(MIMETYPE)){
//Desktop Items getting moved around - already performed in the dragMoveEvent
ev->accept();
}else if(ev->mimeData()->hasUrls()){
ev->accept();
//Files getting dropped here
QList<QUrl> urls = ev->mimeData()->urls();
qDebug() << "Desktop Drop Event:" << urls;
for(int i=0; i<urls.length(); i++){
//If this file is not in the desktop folder, move/copy it here
if(urls[i].isLocalFile()){
QFileInfo info(urls[i].toLocalFile());
if(info.exists() && !QFile::exists(QDir::homePath()+"/Desktop/"+info.fileName())){
//Make a link to the file here
QFile::link(info.absoluteFilePath(), QDir::homePath()+"/Desktop/"+info.fileName());
}else{
qWarning() << "Invalid desktop file drop (ignored):" << urls[i].toString();
}
}
}
}else{
//Ignore this event
ev->ignore();
}
}
};
#endif
|