aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-desktop/desktop-plugins/desktopview/DesktopViewPlugin.cpp
blob: 6f4d62d6aad5070558b882be632696101830bdac (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
#include "DesktopViewPlugin.h"

#include <QFileInfo>
#include <QDir>
#include <QClipboard>
#include <QMimeData>
#include <QImageReader>

#include <LuminaXDG.h>
#include "LSession.h"


DesktopViewPlugin::DesktopViewPlugin(QWidget* parent, QString ID) : LDPlugin(parent, ID){
  this->setLayout( new QVBoxLayout());
    this->layout()->setContentsMargins(0,0,0,0);
	
  list = new QListWidget(this);
    list->setViewMode(QListView::IconMode);
    list->setFlow(QListWidget::TopToBottom); //Qt bug workaround - need the opposite flow in the widget constructor
    list->setWrapping(true);
    list->setSpacing(4);
    list->setSelectionBehavior(QAbstractItemView::SelectItems);
    list->setSelectionMode(QAbstractItemView::ExtendedSelection);
    list->setContextMenuPolicy(Qt::CustomContextMenu);
    list->setMovement(QListView::Snap); //make sure items are "stuck" in the grid
	
  menu = new QMenu(this);
    menu->addAction( LXDG::findIcon("run-build-file",""), tr("Open"), this, SLOT(runItems()) );
    menu->addSeparator();
    menu->addAction( LXDG::findIcon("edit-cut",""), tr("Cut"), this, SLOT(cutItems()) );
    menu->addAction( LXDG::findIcon("edit-copy",""), tr("Copy"), this, SLOT(copyItems()) );
    menu->addSeparator();
    menu->addAction( LXDG::findIcon("zoom-in",""), tr("Increase Icons"), this, SLOT(increaseIconSize()) );
    menu->addAction( LXDG::findIcon("zoom-out",""), tr("Decrease Icons"), this, SLOT(decreaseIconSize()) );
    menu->addSeparator();
    menu->addAction( LXDG::findIcon("edit-delete",""), tr("Delete"), this, SLOT(deleteItems()) );
    menu->addSeparator();
    if(LUtils::isValidBinary("lumina-fileinfo")){
      menu->addAction( LXDG::findIcon("system-search",""), tr("Properties"), this, SLOT(displayProperties()) );
    }
  this->layout()->addWidget(list);
    
  connect(QApplication::instance(), SIGNAL(DesktopFilesChanged()), this, SLOT(updateContents()) );
  connect(list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(runItems()) );
  connect(list, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu(const QPoint&)) );
  QTimer::singleShot(1000,this, SLOT(updateContents()) ); //wait a second before loading contents
}

DesktopViewPlugin::~DesktopViewPlugin(){

}

void DesktopViewPlugin::runItems(){
    QList<QListWidgetItem*> sel = list->selectedItems();
    for(int i=0; i<sel.length(); i++){
      LSession::LaunchApplication("lumina-open \""+sel[i]->whatsThis()+"\"");
    }
}

void DesktopViewPlugin::copyItems(){
  QList<QListWidgetItem*> sel = list->selectedItems();
  if(sel.isEmpty()){ return; } //nothing selected
  QStringList items;
  //Format the data string
  for(int i=0; i<sel.length(); i++){
    items << "copy::::"+sel[i]->whatsThis();
  }
  //Now save that data to the global clipboard
  QMimeData *dat = new QMimeData;
	dat->clear();
	dat->setData("x-special/lumina-copied-files", items.join("\n").toLocal8Bit());
  QApplication::clipboard()->clear();
  QApplication::clipboard()->setMimeData(dat);
}

void DesktopViewPlugin::cutItems(){
  QList<QListWidgetItem*> sel = list->selectedItems();
  if(sel.isEmpty()){ return; } //nothing selected
  QStringList items;
  //Format the data string
  for(int i=0; i<sel.length(); i++){
    items << "cut::::"+sel[i]->whatsThis();
  }
  //Now save that data to the global clipboard
  QMimeData *dat = new QMimeData;
	dat->clear();
	dat->setData("x-special/lumina-copied-files", items.join("\n").toLocal8Bit());
  QApplication::clipboard()->clear();
  QApplication::clipboard()->setMimeData(dat);
}

void DesktopViewPlugin::deleteItems(){
  QList<QListWidgetItem*> sel = list->selectedItems();
  for(int i=0; i<sel.length(); i++){
    if(QFileInfo(sel[i]->whatsThis()).isDir()){
      QProcess::startDetached("rm -r \""+sel[i]->whatsThis()+"\"");
    }else{
      QFile::remove(sel[i]->whatsThis());
    }
  }
}

void DesktopViewPlugin::showMenu(const QPoint &pos){
  //Make sure there is an item underneath the mouse first
  if(list->itemAt(pos)!=0){
    menu->popup(this->mapToGlobal(pos));
  }else{
    //Pass the context menu request on to the desktop (emit it from the plugin)
    this->showPluginMenu();
    //emit OpenDesktopMenu();
  }
}

void DesktopViewPlugin::increaseIconSize(){
  int icosize = this->readSetting("IconSize",64).toInt();
  icosize+=16; //go in orders of 16 pixels
  //list->setIconSize(QSize(icosize,icosize));
  this->saveSetting("IconSize",icosize);
  QTimer::singleShot(10, this, SLOT(updateContents()));
}

void DesktopViewPlugin::decreaseIconSize(){
  int icosize = this->readSetting("IconSize",64).toInt();
  if(icosize < 20){ return; } //too small to decrease more
  icosize-=16; //go in orders of 16 pixels
  //list->setIconSize(QSize(icosize,icosize));
  this->saveSetting("IconSize",icosize);	
  QTimer::singleShot(10,this, SLOT(updateContents()));
}

void DesktopViewPlugin::updateContents(){
  list->clear();

  int icosize = this->readSetting("IconSize",64).toInt();
  QSize gridSZ = QSize(qRound(1.8*icosize),icosize+4+(2*this->fontMetrics().height()) );
  //qDebug() << "Icon Size:" << icosize <<"Grid Size:" << gridSZ.width() << gridSZ.height();
  list->setGridSize(gridSZ);
  list->setIconSize(QSize(icosize,icosize));
  QDir dir(QDir::homePath()+"/Desktop");
  QFileInfoList files = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name | QDir::Type | QDir::DirsFirst);
  for(int i=0; i<files.length(); i++){
    QListWidgetItem *it = new QListWidgetItem;
    it->setSizeHint(gridSZ); //ensure uniform item sizes
    //it->setForeground(QBrush(Qt::black, Qt::Dense2Pattern)); //Try to use a font color which will always be visible
    it->setTextAlignment(Qt::AlignCenter);
    it->setWhatsThis(files[i].absoluteFilePath());
    QString txt;
    if(files[i].isDir()){
	it->setIcon( LXDG::findIcon("folder","") );
	txt = files[i].fileName();
    }else if(files[i].suffix() == "desktop" ){
	XDGDesktop desk(files[i].absoluteFilePath());
	if(desk.isValid()){
	  it->setIcon( LXDG::findIcon(desk.icon,"unknown") );
	  if(desk.name.isEmpty()){
	    txt = files[i].fileName();
	  }else{
            txt = desk.name;
	  }
	}else{
	  //Revert back to a standard file handling
          it->setIcon( LXDG::findMimeIcon(files[i].fileName()) );
          txt = files[i].fileName();		
	}
    }else if(LUtils::imageExtensions().contains(files[i].suffix().toLower()) ){
      it->setIcon( QIcon( QPixmap(files[i].absoluteFilePath()).scaled(icosize,icosize,Qt::IgnoreAspectRatio, Qt::SmoothTransformation) ) );
      txt = files[i].fileName();	    
    }else{
      it->setIcon( LXDG::findMimeIcon( files[i].fileName() ) );
      txt = files[i].fileName();
    }
    //Add the sym-link overlay to the icon as necessary
    if(files[i].isSymLink()){
      QImage img = it->icon().pixmap(QSize(icosize,icosize)).toImage();
      int oSize = icosize/2; //overlay size
      QPixmap overlay = LXDG::findIcon("emblem-symbolic-link").pixmap(oSize,oSize).scaled(oSize,oSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
      QPainter painter(&img);
        painter.drawPixmap(icosize-oSize,icosize-oSize,overlay); //put it in the bottom-right corner
      it->setIcon( QIcon(QPixmap::fromImage(img)) );
    }
    //Now adjust the visible text as necessary based on font/grid sizing
    it->setToolTip(txt);
    if(this->fontMetrics().horizontalAdvance(txt) > (gridSZ.width()-4) ){
      //int dash = this->fontMetrics().horizontalAdvance("-");
      //Text too long, try to show it on two lines
      txt = txt.section(" ",0,2).replace(" ","\n"); //First take care of any natural breaks
      if(txt.contains("\n")){
        //need to check each line
	QStringList txtL = txt.split("\n");
	for(int i=0; i<txtL.length(); i++){ txtL[i] = this->fontMetrics().elidedText(txtL[i], Qt::ElideRight, gridSZ.width()-4); }
	txt = txtL.join("\n");
	if(txtL.length()>2){ txt = txt.section("\n",0,1); } //only keep the first two lines
      }else{
        txt = this->fontMetrics().elidedText(txt,Qt::ElideRight, 2*(gridSZ.width()-4));
        //Now split the line in half for the two lines
        txt.insert( (txt.count()/2), "\n");
      }
    }else{
      txt.append("\n "); //ensure two lines (2nd one invisible) - keeps formatting sane
    }
    it->setText(txt);
    list->addItem(it);
    if( (i%10) == 0){ QApplication::processEvents(); }//keep the UI snappy, every 10 items
  }
  list->setFlow(QListWidget::TopToBottom); //To ensure this is consistent - issues with putting it in the constructor
  list->update(); //Re-paint the widget after all items are added 
}

void DesktopViewPlugin::displayProperties(){
    QList<QListWidgetItem*> sel = list->selectedItems();
    for(int i=0; i<sel.length(); i++){
      LSession::LaunchApplication("lumina-fileinfo \""+sel[i]->whatsThis());
    }
}
bgstack15