MythTV  master
fileselector.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 
3 // Qt
4 #include <QApplication>
5 #include <QDir>
6 #include <QFileInfo>
7 #include <QVariant>
8 
9 // Myth
10 #include <mythcontext.h>
11 #include <mythdb.h>
12 #include <mythuihelper.h>
13 #include <mythuitext.h>
14 #include <mythuitextedit.h>
15 #include <mythuibutton.h>
16 #include <mythuiimage.h>
17 #include <mythuibuttonlist.h>
18 #include <mythmainwindow.h>
19 #include <mythdialogbox.h>
20 
21 // mytharchive
22 #include "fileselector.h"
23 #include "archiveutil.h"
24 
26 
28 {
29  while (!m_fileData.isEmpty())
30  delete m_fileData.takeFirst();
31 }
32 
34 {
35  // Load the theme for this screen
36  bool foundtheme = LoadWindowFromXML("mytharchive-ui.xml", "file_selector", this);
37  if (!foundtheme)
38  return false;
39 
40  bool err = false;
41  UIUtilW::Assign(this, m_titleText, "title_text");
42  UIUtilE::Assign(this, m_fileButtonList, "filelist", &err);
43  UIUtilE::Assign(this, m_locationEdit, "location_edit", &err);
44  UIUtilE::Assign(this, m_backButton, "back_button", &err);
45  UIUtilE::Assign(this, m_homeButton, "home_button", &err);
46  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
47  UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
48 
49  if (err)
50  {
51  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'file_selector'");
52  return false;
53  }
54 
55  if (m_titleText)
56  {
57  switch (m_selectorType)
58  {
59  case FSTYPE_FILE:
60  m_titleText->SetText(tr("Find File"));
61  break;
62  case FSTYPE_DIRECTORY:
63  m_titleText->SetText(tr("Find Directory"));
64  break;
65  default:
66  m_titleText->SetText(tr("Find Files"));
67  break;
68  }
69  }
70 
71  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(OKPressed()));
72  connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(cancelPressed()));
73 
74  connect(m_locationEdit, SIGNAL(LosingFocus()),
75  this, SLOT(locationEditLostFocus()));
77 
78  connect(m_backButton, SIGNAL(Clicked()), this, SLOT(backPressed()));
79  connect(m_homeButton, SIGNAL(Clicked()), this, SLOT(homePressed()));
80 
82  this, SLOT(itemClicked(MythUIButtonListItem *)));
83 
85 
87 
90 
91  return true;
92 }
93 
94 bool FileSelector::keyPressEvent(QKeyEvent *event)
95 {
96  if (GetFocusWidget()->keyPressEvent(event))
97  return true;
98 
99  QStringList actions;
100  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
101 
102  for (int i = 0; i < actions.size() && !handled; i++)
103  {
104  QString action = actions[i];
105  handled = true;
106 
107  if (action == "MENU")
108  {
109 
110  }
111  else
112  handled = false;
113  }
114 
115  if (!handled && MythScreenType::keyPressEvent(event))
116  handled = true;
117 
118  return handled;
119 }
120 
122 {
123  if (!item)
124  return;
125 
126  auto *fileData = item->GetData().value<FileData*>();
127 
128  if (fileData->directory)
129  {
130  if (fileData->filename == "..")
131  {
132  // move up on directory
133  int pos = m_curDirectory.lastIndexOf('/');
134  if (pos > 0)
135  m_curDirectory = m_curDirectory.left(pos);
136  else
137  m_curDirectory = "/";
138  }
139  else
140  {
141  if (!m_curDirectory.endsWith("/"))
142  m_curDirectory += "/";
143  m_curDirectory += fileData->filename;
144  }
145  updateFileList();
146  }
147  else
148  {
150  {
151  QString fullPath = m_curDirectory;
152  if (!fullPath.endsWith("/"))
153  fullPath += "/";
154  fullPath += fileData->filename;
155 
157  {
158  m_selectedList.removeAll(fullPath);
160  }
161  else
162  {
163  if (m_selectedList.indexOf(fullPath) == -1)
164  m_selectedList.append(fullPath);
166  }
167  }
168  }
169 }
170 
172 {
173  return m_curDirectory;
174 }
175 
177 {
179  updateFileList();
180 }
181 
183 {
184  // move up one directory
185  int pos = m_curDirectory.lastIndexOf('/');
186  if (pos > 0)
187  m_curDirectory = m_curDirectory.left(pos);
188  else
189  m_curDirectory = "/";
190 
191  updateFileList();
192 }
193 
195 {
196  char *home = getenv("HOME");
197  m_curDirectory = home;
198 
199  updateFileList();
200 }
201 
203 {
205  {
206  // loop though selected files and add them to the list
207  QString f;
208 
209  // remove any items that have been removed from the list
210  QList<ArchiveItem *> tempAList;
211  for (auto *a : qAsConst(*m_archiveList))
212  {
213  bool found = false;
214 
215  for (int y = 0; y < m_selectedList.size(); y++)
216  {
217  f = m_selectedList.at(y);
218  if (a->type != "File" || a->filename == f)
219  {
220  found = true;
221  break;
222  }
223  }
224 
225  if (!found)
226  tempAList.append(a);
227  }
228 
229  for (auto *x : qAsConst(tempAList))
230  m_archiveList->removeAll(x);
231 
232  // remove any items that are already in the list
233  QStringList tempSList;
234  for (int x = 0; x < m_selectedList.size(); x++)
235  {
236  f = m_selectedList.at(x);
237 
238  for (const auto *a : qAsConst(*m_archiveList))
239  {
240  if (a->filename == f)
241  {
242  tempSList.append(f);
243  break;
244  }
245  }
246  }
247 
248  for (int x = 0; x < tempSList.size(); x++)
249  m_selectedList.removeAll(tempSList.at(x));
250 
251  // add all that are left
252  for (int x = 0; x < m_selectedList.size(); x++)
253  {
254  f = m_selectedList.at(x);
255 
256  QFile file(f);
257  if (file.exists())
258  {
259  QString title = f;
260  int pos = f.lastIndexOf('/');
261  if (pos > 0)
262  title = f.mid(pos + 1);
263 
264  auto *a = new ArchiveItem;
265  a->type = "File";
266  a->title = title;
267  a->subtitle = "";
268  a->description = "";
269  a->startDate = "";
270  a->startTime = "";
271  a->size = (int64_t)file.size();
272  a->filename = f;
273  a->hasCutlist = false;
274  a->useCutlist = false;
275  a->duration = 0;
276  a->cutDuration = 0;
277  a->videoWidth = 0;
278  a->videoHeight = 0;
279  a->fileCodec = "";
280  a->videoCodec = "";
281  a->encoderProfile = nullptr;
282  a->editedDetails = false;
283  m_archiveList->append(a);
284  }
285  }
286  }
287  else
288  {
290  auto *fileData = item->GetData().value<FileData*>();
291 
293  {
294  if (!fileData->directory)
295  {
296  ShowOkPopup(tr("The selected item is not a directory!"));
297  return;
298  }
299 
300  if (fileData->filename != "..")
301  {
302  if (!m_curDirectory.endsWith("/"))
303  m_curDirectory += "/";
304  m_curDirectory += fileData->filename;
305  }
306  }
307  else
308  {
309  if (fileData->directory)
310  {
311  if (!m_curDirectory.endsWith("/"))
312  m_curDirectory += "/";
313  }
314  else
315  {
316  if (!m_curDirectory.endsWith("/"))
317  m_curDirectory += "/";
318  m_curDirectory += fileData->filename;
319  }
320  }
321  }
322 
324  emit haveResult(true);
325  else
326  emit haveResult(getSelected());
327  Close();
328 }
329 
331 {
333  emit haveResult(true);
334  else
335  emit haveResult(QString());
336  Close();
337 }
338 
340 {
341  if (!m_archiveList)
342  return;
343 
344  while (!m_selectedList.isEmpty())
345  m_selectedList.takeFirst();
346  m_selectedList.clear();
347 
348  for (const auto *a : qAsConst(*m_archiveList))
349  {
350  for (const auto *f : qAsConst(m_fileData))
351  {
352  if (f->filename == a->filename)
353  {
354  if (m_selectedList.indexOf(f->filename) == -1)
355  m_selectedList.append(f->filename);
356  break;
357  }
358  }
359  }
360 }
361 
363 {
364  if (!m_fileButtonList)
365  return;
366 
368  while (!m_fileData.isEmpty())
369  delete m_fileData.takeFirst();
370  m_fileData.clear();
371 
372  QDir d;
373 
374  d.setPath(m_curDirectory);
375  if (d.exists())
376  {
377  // first get a list of directory's in the current directory
378  QStringList filters;
379  filters << "*";
380  QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);
381 
382  for (const auto & fi : qAsConst(list))
383  {
384  if (fi.fileName() != ".")
385  {
386  auto *data = new FileData;
387  data->selected = false;
388  data->directory = true;
389  data->filename = fi.fileName();
390  data->size = 0;
391  m_fileData.append(data);
392 
393  // add a row to the MythUIButtonList
394  auto* item = new
395  MythUIButtonListItem(m_fileButtonList, data->filename);
396  item->setCheckable(false);
397  item->SetImage("ma_folder.png");
398  item->SetData(QVariant::fromValue(data));
399  }
400  }
401 
403  {
404  // second get a list of file's in the current directory
405  filters.clear();
406 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
407  filters = m_filemask.split(" ", QString::SkipEmptyParts);
408 #else
409  filters = m_filemask.split(" ", Qt::SkipEmptyParts);
410 #endif
411  list = d.entryInfoList(filters, QDir::Files, QDir::Name);
412  for (const auto & fi : qAsConst(list))
413  {
414  auto *data = new FileData;
415  data->selected = false;
416  data->directory = false;
417  data->filename = fi.fileName();
418  data->size = fi.size();
419  m_fileData.append(data);
420 
421  // add a row to the UIListBtnArea
422  auto* item =
423  new MythUIButtonListItem(m_fileButtonList, data->filename);
424  item->SetText(formatSize(data->size / 1024, 2), "size");
425 
427  {
428  item->setCheckable(true);
429 
430  QString fullPath = m_curDirectory;
431  if (!fullPath.endsWith("/"))
432  fullPath += "/";
433  fullPath += data->filename;
434 
435  if (m_selectedList.indexOf(fullPath) != -1)
436  {
437  item->setChecked(MythUIButtonListItem::FullChecked);
438  }
439  else
440  {
441  item->setChecked(MythUIButtonListItem::NotChecked);
442  }
443  }
444  else
445  item->setCheckable(false);
446 
447  item->SetData(QVariant::fromValue(data));
448  }
449  }
451  }
452  else
453  {
454  m_locationEdit->SetText("/");
455  LOG(VB_GENERAL, LOG_ERR,
456  "MythArchive: current directory does not exist!");
457  }
458 }
FSTYPE_FILE
@ FSTYPE_FILE
Definition: fileselector.h:29
FileData::selected
bool selected
Definition: fileselector.h:21
FileSelector::backPressed
void backPressed(void)
Definition: fileselector.cpp:182
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
mythuitext.h
FileData
Definition: fileselector.h:19
fileselector.h
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:550
FileSelector::itemClicked
void itemClicked(MythUIButtonListItem *item)
Definition: fileselector.cpp:121
mythdb.h
FileSelector::Create
bool Create(void) override
Definition: fileselector.cpp:33
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
FileSelector::updateFileList
void updateFileList(void)
Definition: fileselector.cpp:362
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:633
FileSelector::homePressed
void homePressed(void)
Definition: fileselector.cpp:194
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1106
d
static const uint16_t * d
Definition: iso6937tables.cpp:1025
mythdialogbox.h
FileSelector::keyPressEvent
bool keyPressEvent(QKeyEvent *e) override
Key event handler.
Definition: fileselector.cpp:94
MythUIButtonListItem::FullChecked
@ FullChecked
Definition: mythuibuttonlist.h:34
FileSelector::m_fileData
QList< FileData * > m_fileData
Definition: fileselector.h:80
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythUITextEdit::GetText
QString GetText(void) const
Definition: mythuitextedit.h:47
build_compdb.file
file
Definition: build_compdb.py:55
mythuibuttonlist.h
mythuiimage.h
FileSelector::m_backButton
MythUIButton * m_backButton
Definition: fileselector.h:91
FileSelector::m_titleText
MythUIText * m_titleText
Definition: fileselector.h:86
FileSelector::getSelected
QString getSelected(void)
Definition: fileselector.cpp:171
FSTYPE_DIRECTORY
@ FSTYPE_DIRECTORY
Definition: fileselector.h:30
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
FileSelector::cancelPressed
void cancelPressed(void)
Definition: fileselector.cpp:330
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
MythUITextEdit::SetText
void SetText(const QString &text, bool moveCursor=true)
Definition: mythuitextedit.cpp:216
ArchiveItem::type
QString type
Definition: archiveutil.h:54
archiveutil.h
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
FileSelector::m_selectedList
QStringList m_selectedList
Definition: fileselector.h:81
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
FileSelector::m_filemask
QString m_filemask
Definition: fileselector.h:78
FileSelector::m_archiveList
QList< ArchiveItem * > * m_archiveList
Definition: fileselector.h:82
FileSelector::locationEditLostFocus
void locationEditLostFocus(void)
Definition: fileselector.cpp:176
f
QTextStream t & f
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:600
FileSelector::m_locationEdit
MythUITextEdit * m_locationEdit
Definition: fileselector.h:88
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3588
FileSelector::updateSelectedList
void updateSelectedList(void)
Definition: fileselector.cpp:339
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
FileSelector::m_okButton
MythUIButton * m_okButton
Definition: fileselector.h:89
FileSelector::OKPressed
void OKPressed(void)
Definition: fileselector.cpp:202
mythuihelper.h
FileSelector::~FileSelector
~FileSelector() override
Definition: fileselector.cpp:27
FileSelector::m_selectorType
FSTYPE m_selectorType
Definition: fileselector.h:77
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:414
mythuitextedit.h
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
FileSelector::haveResult
void haveResult(bool ok)
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:134
MythUIButtonListItem::state
CheckState state() const
Definition: mythuibuttonlist.cpp:3542
mythcontext.h
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:107
build_compdb.action
action
Definition: build_compdb.py:9
FileSelector::m_curDirectory
QString m_curDirectory
Definition: fileselector.h:79
mythuibutton.h
FSTYPE_FILELIST
@ FSTYPE_FILELIST
Definition: fileselector.h:28
FileSelector::m_homeButton
MythUIButton * m_homeButton
Definition: fileselector.h:92
formatSize
static QString formatSize(int64_t sizeKB, int prec)
Definition: recordingutils.cpp:23
ArchiveItem
Definition: archiveutil.h:52
MythUIType::LosingFocus
void LosingFocus()
FileSelector::m_fileButtonList
MythUIButtonList * m_fileButtonList
Definition: fileselector.h:87
mythmainwindow.h
MythUIButtonListItem::setChecked
void setChecked(CheckState state)
Definition: mythuibuttonlist.cpp:3552
FileSelector::m_cancelButton
MythUIButton * m_cancelButton
Definition: fileselector.h:90
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:32