aboutsummaryrefslogtreecommitdiff
path: root/lumina-desktop/LDesktopPluginSpace.h
blob: 842999b3b59cf0ca471546cd608be90d913124b3 (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
//===========================================
//  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 "desktop-plugins/LDPlugin.h"

#define MIMETYPE QString("x-special/lumina-desktop-plugin")
//#define MIMEPOS QString("x-special/lumina-desktop-plugin-pos")
#define GRIDSIZE 16.0 //Need this to be a double/float - usually used for divisions

class LDesktopPluginSpace : public QWidget{
  	Q_OBJECT
	
signals:
	void PluginRemovedByUser(QString ID);
	void IncreaseIcons(); //increase default icon sizes
	void DecreaseIcons(); //decrease default icon sizes
	
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();

private:
	QSettings *plugsettings;
	QSize itemSize;
	QStringList plugins, deskitems;	
	QList<LDPlugin*> ITEMS;
	bool TopToBottom;

	int RoundUp(double num){
	 int out = num; //This will truncate the number
	 if(out < num){ out++; } //need to increase by 1
	 return out;
	}

	QSize calculateItemSize(int icosize);
	void addDesktopItem(QString filepath); //This will convert it into a valid Plugin ID
	void addDesktopPlugin(QString plugID);


	QPoint findOpenSpot(int gridwidth = 1, int gridheight = 1, int startRow = 0, int startCol = 0);
	
	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){
	  return QRect( RoundUp(geom.x()/GRIDSIZE), RoundUp(geom.y()/GRIDSIZE), \
			RoundUp(geom.width()/GRIDSIZE), RoundUp(geom.height()/GRIDSIZE) );
		
	}
	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)
	  //qDebug() << "GridToGeom:" << grid << geom;
	  if(geom.right() > this->geometry().right() && (geom.right() -this->geometry().right())<GRIDSIZE ){
	    geom.setRight(this->geometry().right()); //match up with the edge
	  }
	  if(geom.bottom() > this->geometry().bottom() && (geom.bottom() -this->geometry().bottom())<GRIDSIZE ){
	    geom.setBottom(this->geometry().bottom()); //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 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;
	}
	
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){
	  for(int i=0; i<ITEMS.length(); i++){
	    if(ITEMS[i]->whatsThis()==ID){
	      ITEMS[i]->Cleanup();
	      delete ITEMS.takeAt(i);
	      emit PluginRemovedByUser(ID);
	      return;
	    }
	  }
	}

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
		  geom.moveTo( (geom.topLeft()+diff) );
		  geom = gridToGeom(geom); //convert back to px coords with edge matching
		  //qDebug() << " - Setting Geometry:" << geom;
		  if(ValidGeometry(act.section("::::",1,50), geom)){ 
		    item->setGeometry(geom); 
		    ev->acceptProposedAction(); 
		    item->savePluginGeometry(geomToGrid(geom));
		  }else{ ev->ignore(); } //invalid location
		  
	      }else{
		//Resize operation
		QPoint diff = grid - posToGrid(geom.center());; //need difference from center (grid)
		geom = geomToGrid(geom); //convert to grid coordinates now
		if(diff.x()==0 && diff.y()==0){
		  ev->acceptProposedAction(); //nothing to do - but keep the drag active
		}else{
		  if(diff.x()<0){ geom.setLeft( grid.x()); } //expanding to the left
		  else if(diff.x()>0){ geom.setRight( grid.x()); } //expanding to the right
		  if(diff.y()<0){ geom.setTop( grid.y()); } //expanding above
		  else if(diff.y()>0){ geom.setBottom( grid.y()); } //expanding below
		  //Now convert back to pixel coords (includes edge matching/adjustments)
		  geom = gridToGeom(geom);
		  //Check Validity of new geom
		  if(ValidGeometry(act.section("::::",1,50), geom)){ 
		    item->setGeometry(geom); 
		    ev->acceptProposedAction();
		    item->savePluginGeometry(geomToGrid(geom));
	          }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
bgstack15