QXRD  0.11.16
qxrdfilebrowsermodel.cpp
Go to the documentation of this file.
1 #include "qxrdfilebrowsermodel.h"
2 #include <QTime>
3 #include <stdio.h>
4 #include <QStringList>
5 #include <QDirIterator>
6 #include <QSize>
7 #include <QPixmap>
8 #include "qcepdebug.h"
9 #include "qxrdapplication.h"
12 #include "qcepmutexlocker.h"
13 
15  QAbstractTableModel(parent),
16  m_Mutex(QMutex::Recursive),
17  m_UpdaterThread(NULL),
18  m_Updater(),
19  m_SortedColumn(0),
20  m_SortOrder(Qt::AscendingOrder),
21  m_Limit(1000),
22  m_TrueSize(0),
23  m_HighlightOnTime(0.5),
26  m_HighlightHue(116)
27 {
28 }
29 
31 {
32 #ifndef QT_NO_DEBUG
33  printf("Deleting file browser model\n");
34 #endif
35 
36 // if (m_UpdaterThread) {
37 // m_UpdaterThread->shutdown();
38 // }
39 }
40 
42 {
46  m_UpdaterThread -> setModel(sharedFromThis());
47 // m_UpdaterThread -> setObjectName("browser");
48  m_UpdaterThread -> start();
49  m_Updater = m_UpdaterThread->updater();
50 }
51 
53  (int section, Qt::Orientation orientation, int role) const
54 {
55  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
56 
57  if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
58  switch (section) {
59  case 0:
60  return "File name";
61 
62  case 1:
63  return "Size";
64 
65  case 2:
66  return "Modified";
67 
68  default:
69  return "";
70  }
71  } else {
72  return inherited::headerData(section, orientation, role);
73  }
74 
75  return QVariant();
76 }
77 
78 QVariant QxrdFileBrowserModel::data(const QModelIndex &idx, int role) const
79 {
80  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
81 
82  QModelIndex index = idx;
83  QFileInfo info = fileInfo(index);
84 
85  if (role == Qt::DisplayRole) {
86  if (index.isValid()) {
87  if (index.column() == 0) {
88  return info.fileName();
89  } else if (index.column() == 1) {
90  if (info.isDir()) {
91  return "--";
92  } else if (!info.exists()) {
93  return "";
94  } else {
95  qint64 sz = info.size();
96 
97  if (sz < 1024) {
98  return tr("%1 B").arg(sz);
99  } else if (sz < 1024*1024) {
100  return tr("%1 KB").arg(sz/1024);
101  } else if (sz < 1024*1024*1024) {
102  return tr("%1 MB").arg(sz/(1024*1024));
103  } else {
104  return tr("%1 GB").arg(sz/(1024*1024*1024));
105  }
106  }
107  } else if (index.column() == 2) {
108  return info.lastModified();
109  }
110  }
111  } else if (role == Qt::DecorationRole) {
112  if (index.column() == 0) {
113  if (info.isDir()) {
114  return QPixmap(":/images/folder-16x16.png");
115  } else if (!info.exists()) {
116  return QVariant();
117  } else {
118  QString suffix = info.suffix();
119 
120  if (suffix == "metadata") {
121  return QPixmap(":/images/file-metadata-16x16.png");
122  } else if (suffix == "avg") {
123  return QPixmap(":/images/file-integration-16x16.png");
124  } else if (suffix == "tif") {
125  return QPixmap(":/images/file-image-16x16.png");
126  } else {
127  return QPixmap(":/images/file-generic-16x16.png");
128  }
129  }
130  }
131  // } else if (role == Qt::SizeHintRole) {
132  // return QSize(80,10);
133  } else if (role == Qt::BackgroundRole) {
134  double lastMod = info.lastModified().msecsTo(QDateTime::currentDateTime())/1000.0;
135 
136  if (lastMod > (m_HighlightOnTime+m_HighlightFadeTime)) {
137  return QColor(Qt::white);
138  } else if (info.exists()){
139  QxrdFileBrowserModel *model = const_cast<QxrdFileBrowserModel*>(this);
140  emit model->dataChanged(index, index);
141 // printf("Data %d changed after %g\n", index.row(), lastMod);
142 
143 // m_Updater -> needUpdate();
144 
145  if (lastMod > m_HighlightOnTime) {
146  double fade = lastMod - m_HighlightOnTime;
147 
148  return QColor::fromHsv(m_HighlightHue, int(m_HighlightSaturation*double(m_HighlightFadeTime-fade)/double(m_HighlightFadeTime)), 255, 255);
149  } else {
150  return QColor::fromHsv(m_HighlightHue, m_HighlightSaturation, 255, 255);
151  }
152  }
153  }
154 
155  return QVariant();
156 }
157 
158 int QxrdFileBrowserModel::columnCount ( const QModelIndex & /*parent*/ ) const
159 {
160  return 3;
161 }
162 
163 int QxrdFileBrowserModel::rowCount ( const QModelIndex & /*parent*/ ) const
164 {
165  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
166 
167  if (m_Limit > 0) {
168  return m_DirList.count() + m_Limit + 1;
169  } else {
170  return m_FileList.count() + m_DirList.count();
171  }
172 }
173 
174 void QxrdFileBrowserModel::setNameFilters(QStringList filters)
175 {
176  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
177 
178  m_NameFilters = filters;
179 
180  updateModel();
181 }
182 
184 {
185 }
186 
187 QFileInfo QxrdFileBrowserModel::fileInfo(const QModelIndex &index) const
188 {
189  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
190 
191  int n = index.row();
192  QFileInfo info;
193 
194  if (n >= m_DirList.count()) {
195  int nf = n-m_DirList.count();
196 
197  if (nf >= m_FileList.count()) {
198  info = QFileInfo(tr("... %1 additional files not displayed...").arg(m_TrueSize-m_Limit));
199  } else {
200  info = m_FileList.at(nf);
201  }
202  } else {
203  info = m_DirList.at(n);
204  }
205 
206  return info;
207 }
208 
209 QString QxrdFileBrowserModel::fileName(const QModelIndex &index) const
210 {
211  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
212 
213  return fileInfo(index).fileName();
214 }
215 
216 QString QxrdFileBrowserModel::filePath(const QModelIndex &index) const
217 {
218  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
219 
220  return fileInfo(index).filePath();
221 }
222 
224 {
225  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
226 
227  m_RootPath = path;
228 
229  updateModel();
230 
231  emit rootChanged(m_RootPath);
232 }
233 
235 {
236  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
237 
238  return m_RootPath;
239 }
240 
242 {
243  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
244 
245  return m_NameFilters;
246 }
247 
249 {
250  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
251 
252  return m_SortedColumn;
253 }
254 
255 Qt::SortOrder QxrdFileBrowserModel::sortOrder() const
256 {
257  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
258 
259  return m_SortOrder;
260 }
261 
263 {
264  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
265 
266  updateModel();
267 }
268 
270 {
271  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
272 
274 
275  if (updater) {
276  updater -> needUpdate();
277  }
278 }
279 
280 bool QxrdFileBrowserModel::isDir(const QModelIndex &index) const
281 {
282  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
283 
284  return fileInfo(index).isDir();
285 }
286 
287 void QxrdFileBrowserModel::sort (int column, Qt::SortOrder order)
288 {
289  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
290 
291  if ((m_SortedColumn != column) || (m_SortOrder != order)) {
292  m_SortedColumn = column;
293  m_SortOrder = order;
294 
296 
297  if (updater) {
298  updater -> needUpdate();
299  }
300  }
301 }
302 
303 void QxrdFileBrowserModel::newDataAvailable(QVector<QFileInfo> dirs, QVector<QFileInfo> files, int limit, int trueSize)
304 {
305  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
306 
307  beginResetModel();
308 
309  m_Limit = limit;
310  m_TrueSize = trueSize;
311  m_DirList = dirs;
312  m_FileList = files;
313 
314  endResetModel();
315 }
316 
318 {
319 // QxrdExperimentPtr expt(m_Experiment);
320 
321 // if (expt && qcepDebug(DEBUG_BROWSER)) {
322 // expt->printMessage(tr("file %1 updated at %2")
323 // .arg(path)
324 // .arg(atTime.toString(Qt::ISODate)));
325 // }
326 
327  emit fileUpdated(file);
328 }
329 
331 {
332  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
333 
335 
336  if (updater) {
337  updater -> generateFileUpdates(doIt);
338  }
339 }
QxrdFileBrowserModel(QObject *parent)
QxrdFileBrowserModelUpdaterWPtr m_Updater
QFileInfo fileInfo(const QModelIndex &index) const
QStringList nameFilters() const
QxrdFileBrowserModelUpdaterThreadPtr m_UpdaterThread
QVector< QFileInfo > m_FileList
int rowCount(const QModelIndex &parent=QModelIndex()) const
int columnCount(const QModelIndex &parent=QModelIndex()) const
void setNameFilters(QStringList filters)
void generateFileUpdates(int doIt)
QVector< QFileInfo > m_DirList
QString fileName(const QModelIndex &index) const
void newDataAvailable(QVector< QFileInfo > dirs, QVector< QFileInfo > files, int limit=0, int trueSize=-1)
QSharedPointer< QxrdFileBrowserModelUpdater > QxrdFileBrowserModelUpdaterPtr
QString filePath(const QModelIndex &index) const
Qt::SortOrder sortOrder() const
bool isDir(const QModelIndex &index) const
void fileUpdated(QFileInfo file)
void setRootPath(QString path)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
void setNameFilterDisables(bool disables)
void rootChanged(const QString &path)
virtual void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
QSharedPointer< QxrdFileBrowserModelUpdaterThread > QxrdFileBrowserModelUpdaterThreadPtr
void updatedFile(QFileInfo file)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const