aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-fm/BrowserWidget.cpp
blob: bec996672a1e4c52889bfda3f41232d47361fa5c (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
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2016, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "BrowserWidget.h"

#include <QVBoxLayout>
#include <QTimer>
#include <QSettings>

#include <LuminaUtils.h>

BrowserWidget::BrowserWidget(QString objID, QWidget *parent) : QWidget(parent){
  //Setup the Widget/UI
  this->setLayout( new QVBoxLayout(this) );
  ID = objID;
  //this->setWhatsThis(objID);
  //Setup the backend browser object
  BROWSER = new Browser(this);
  connect(BROWSER, SIGNAL(clearItems()), this, SLOT(clearItems()) );
  connect(BROWSER, SIGNAL(itemUpdated(QString)), this, SLOT(itemUpdated(QString)) );
  connect(BROWSER, SIGNAL(itemDataAvailable(QIcon, LFileInfo)), this, SLOT(itemDataAvailable(QIcon, LFileInfo)) );
  connect(BROWSER, SIGNAL(itemsLoading(int)), this, SLOT(itemsLoading(int)) );
  connect(this, SIGNAL(dirChange(QString)), BROWSER, SLOT(loadDirectory(QString)) );
  listWidget = 0;
  treeWidget = 0;
  readDateFormat();
}

BrowserWidget::~BrowserWidget(){

}

void BrowserWidget::changeDirectory(QString dir){
  if(BROWSER->currentDirectory()==dir){ return; } //already on this directory
  emit dirChange(dir);
}

void BrowserWidget::showDetails(bool show){
  //Clean up widgets first
  if(show && listWidget!=0){
    //Clean up list widget
    listWidget->deleteLater();
    listWidget = 0;
  }else if(!show && treeWidget!=0){
    treeWidget->deleteLater();
    treeWidget = 0;
  }
  //Now create any new widgets
  if(show && treeWidget == 0){
    treeWidget = new DDTreeWidget(this);
    this->layout()->addWidget(treeWidget);
    retranslate();
    if(!BROWSER->currentDirectory().isEmpty()){ emit dirChange(""); }
  }else if(!show && listWidget==0){
    listWidget = new DDListWidget(this);
    this->layout()->addWidget(listWidget);
    if(!BROWSER->currentDirectory().isEmpty()){ emit dirChange(""); }
  }
}

bool BrowserWidget::hasDetails(){
  return (treeWidget!=0);
}

void BrowserWidget::setThumbnailSize(int px){
  bool larger = true;
  if(listWidget!=0){ 
    larger = listWidget->iconSize().height() < px;
    listWidget->setIconSize(QSize(px,px));
  }else if(treeWidget!=0){
    larger = treeWidget->iconSize().height() < px;
    treeWidget->setIconSize(QSize(px,px));
  }
  if(BROWSER->currentDirectory().isEmpty() || !larger ){ return; } //don't need to reload icons unless the new size is larger
  emit dirChange("");
}

QStringList BrowserWidget::getDateFormat() {
  return date_format;
}

// This function is only called if user changes sessionsettings. By doing so, operations like sorting by date
// are faster because the date format is already stored in DirWidget::date_format static variable
void BrowserWidget::readDateFormat() {
  if(!date_format.isEmpty())
      date_format.clear();
  QSettings settings("lumina-desktop","sessionsettings");
  // If value doesn't exist or is not setted, empty string is returned
  date_format << settings.value("DateFormat").toString();
  date_format << settings.value("TimeFormat").toString();
}

// =================
//     PUBLIC SLOTS
// =================
void BrowserWidget::retranslate(){
  if(listWidget!=0){

  }else if(treeWidget!=0){
    QTreeWidgetItem *it = new QTreeWidgetItem();
    it->setText(0,tr("Name"));
    it->setText(1,tr("Size"));
    it->setText(2, tr("Type"));
    it->setText(3, tr("Date Modified") );
    it->setText(4, tr("Date Created") );
    treeWidget->setHeaderItem(it);
    //Now reset the sorting (alphabetically, dirs first)
    treeWidget->sortItems(0, Qt::AscendingOrder);  // sort by name
    treeWidget->sortItems(1, Qt::AscendingOrder);  //sort by type
  }
}

// =================
//          PRIVATE
// =================
QString BrowserWidget::DTtoString(QDateTime dt){
  QStringList fmt = getDateFormat();
  if(fmt.isEmpty() || fmt.length()!=2 || (fmt[0].isEmpty() && fmt[1].isEmpty()) ){
    //Default formatting
    return dt.toString(Qt::DefaultLocaleShortDate);
  }else if(fmt[0].isEmpty()){
    //Time format only
    return (dt.date().toString(Qt::DefaultLocaleShortDate)+" "+dt.time().toString(fmt[1]));
  }else if(fmt[1].isEmpty()){
    //Date format only
    return (dt.date().toString(fmt[0])+" "+dt.time().toString(Qt::DefaultLocaleShortDate));
  }else{
    //both date/time formats set
    return dt.toString(fmt.join(" "));
  }
}

// =================
//    PRIVATE SLOTS
// =================
void BrowserWidget::clearItems(){
  if(listWidget!=0){ listWidget->clear(); }
  else if(treeWidget!=0){ treeWidget->clear(); }
  this->setEnabled(false);
}

void BrowserWidget::itemRemoved(QString item){
  //qDebug() << "item removed" << item;
  if(treeWidget!=0){
    QList<QTreeWidgetItem*> found = treeWidget->findItems(item.section("/",-1), Qt::MatchExactly, 0); //look for exact name match
    if(found.isEmpty()){ return; } //no match
    delete found[0];
  }else if(listWidget!=0){
    QList<QListWidgetItem*> found = listWidget->findItems(item.section("/",-1), Qt::MatchExactly); //look for exact name match
    if(found.isEmpty()){ return; }
    delete found[0];
  }
}

void BrowserWidget::itemDataAvailable(QIcon ico, LFileInfo info){
  qDebug() << "Item Data Available:" << info.fileName();
  int num = 0;
  if(listWidget!=0){
    //LIST WIDGET - name and icon only
    if(!listWidget->findItems(info.fileName(), Qt::MatchExactly).isEmpty()){ 
      //Update existing item
      QListWidgetItem *it = listWidget->findItems(info.fileName(), Qt::MatchExactly).first();
      it->setText(info.fileName());
      it->setWhatsThis(info.absoluteFilePath());
      it->setIcon(ico);
    }else{
      //New item
      listWidget->addItem( new QListWidgetItem(ico, info.fileName(), listWidget) );
    }
    num = listWidget->count();
  }else if(treeWidget!=0){
    QTreeWidgetItem *it = 0;
    if( ! treeWidget->findItems(info.fileName(), Qt::MatchExactly, 0).isEmpty() ){ it =  treeWidget->findItems(info.fileName(), Qt::MatchExactly, 0).first(); }
    else{ 
      it = new CQTreeWidgetItem(treeWidget);  
      it->setText(0, info.fileName() ); //name (0)
      treeWidget->addTopLevelItem(it);
    }
    //Now set/update all the data
    it->setText(1, LUtils::BytesToDisplaySize(info.size()) ); //size (1)
    it->setText(2, info.mimetype() ); //type (2)
    it->setText(3, DTtoString(info.lastModified() )); //modification date (3)
    it->setText(4, DTtoString(info.created()) ); //creation date (4)
    //Now all the hidden data
    it->setWhatsThis(0, info.absoluteFilePath());
    it->setWhatsThis(3, info.lastModified().toString("yyyyMMddhhmmsszzz") ); //sorts by this actually
    it->setWhatsThis(4, info.created().toString("yyyyMMddhhmmsszzz") ); //sorts by this actually
    num = treeWidget->topLevelItemCount();
  }
  if(num < numItems){
    //Still loading items
    //this->setEnabled(false);
  }else{
    //Done loading items
    this->setEnabled(true);
  }
}

void BrowserWidget::itemsLoading(int total){
  numItems = total; //save this for later
}
bgstack15