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 
25 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
26 #define qEnvironmentVariable getenv
27 #endif
28 
30 
32 {
33  while (!m_fileData.isEmpty())
34  delete m_fileData.takeFirst();
35 }
36 
38 {
39  // Load the theme for this screen
40  bool foundtheme = LoadWindowFromXML("mytharchive-ui.xml", "file_selector", this);
41  if (!foundtheme)
42  return false;
43 
44  bool err = false;
45  UIUtilW::Assign(this, m_titleText, "title_text");
46  UIUtilE::Assign(this, m_fileButtonList, "filelist", &err);
47  UIUtilE::Assign(this, m_locationEdit, "location_edit", &err);
48  UIUtilE::Assign(this, m_backButton, "back_button", &err);
49  UIUtilE::Assign(this, m_homeButton, "home_button", &err);
50  UIUtilE::Assign(this, m_okButton, "ok_button", &err);
51  UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
52 
53  if (err)
54  {
55  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'file_selector'");
56  return false;
57  }
58 
59  if (m_titleText)
60  {
61  switch (m_selectorType)
62  {
63  case FSTYPE_FILE:
64  m_titleText->SetText(tr("Find File"));
65  break;
66  case FSTYPE_DIRECTORY:
67  m_titleText->SetText(tr("Find Directory"));
68  break;
69  default:
70  m_titleText->SetText(tr("Find Files"));
71  break;
72  }
73  }
74 
77 
81 
84 
87 
89 
91 
94 
95  return true;
96 }
97 
98 bool FileSelector::keyPressEvent(QKeyEvent *event)
99 {
100  if (GetFocusWidget()->keyPressEvent(event))
101  return true;
102 
103  QStringList actions;
104  bool handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
105 
106  for (int i = 0; i < actions.size() && !handled; i++)
107  {
108  QString action = actions[i];
109  handled = true;
110 
111  if (action == "MENU")
112  {
113 
114  }
115  else
116  handled = false;
117  }
118 
119  if (!handled && MythScreenType::keyPressEvent(event))
120  handled = true;
121 
122  return handled;
123 }
124 
126 {
127  if (!item)
128  return;
129 
130  auto *fileData = item->GetData().value<FileData*>();
131  if (!fileData)
132  return;
133 
134  if (fileData->directory)
135  {
136  if (fileData->filename == "..")
137  {
138  // move up on directory
139  int pos = m_curDirectory.lastIndexOf('/');
140  if (pos > 0)
141  m_curDirectory = m_curDirectory.left(pos);
142  else
143  m_curDirectory = "/";
144  }
145  else
146  {
147  if (!m_curDirectory.endsWith("/"))
148  m_curDirectory += "/";
149  m_curDirectory += fileData->filename;
150  }
151  updateFileList();
152  }
153  else
154  {
156  {
157  QString fullPath = m_curDirectory;
158  if (!fullPath.endsWith("/"))
159  fullPath += "/";
160  fullPath += fileData->filename;
161 
163  {
164  m_selectedList.removeAll(fullPath);
166  }
167  else
168  {
169  if (m_selectedList.indexOf(fullPath) == -1)
170  m_selectedList.append(fullPath);
172  }
173  }
174  }
175 }
176 
178 {
179  return m_curDirectory;
180 }
181 
183 {
185  updateFileList();
186 }
187 
189 {
190  // move up one directory
191  int pos = m_curDirectory.lastIndexOf('/');
192  if (pos > 0)
193  m_curDirectory = m_curDirectory.left(pos);
194  else
195  m_curDirectory = "/";
196 
197  updateFileList();
198 }
199 
201 {
202  m_curDirectory = qEnvironmentVariable("HOME");
203 
204  updateFileList();
205 }
206 
208 {
210  {
211  // loop though selected files and add them to the list
212  QString f;
213 
214  // remove any items that have been removed from the list
215  QList<ArchiveItem *> tempAList;
216  for (auto *a : qAsConst(*m_archiveList))
217  {
218  bool found = false;
219 
220  for (int y = 0; y < m_selectedList.size(); y++)
221  {
222  f = m_selectedList.at(y);
223  if (a->type != "File" || a->filename == f)
224  {
225  found = true;
226  break;
227  }
228  }
229 
230  if (!found)
231  tempAList.append(a);
232  }
233 
234  for (auto *x : qAsConst(tempAList))
235  m_archiveList->removeAll(x);
236 
237  // remove any items that are already in the list
238  QStringList tempSList;
239  for (int x = 0; x < m_selectedList.size(); x++)
240  {
241  f = m_selectedList.at(x);
242 
243  auto namematch = [f](const auto *a){ return a->filename == f; };
244  if (std::any_of(m_archiveList->cbegin(), m_archiveList->cend(), namematch))
245  tempSList.append(f);
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  auto samename = [a](const auto *f)
351  { return f->filename == a->filename; };
352  auto f = std::find_if(m_fileData.cbegin(), m_fileData.cend(), samename);
353  if (f != m_fileData.cend())
354  {
355  if (m_selectedList.indexOf((*f)->filename) == -1)
356  m_selectedList.append((*f)->filename);
357  }
358  }
359 }
360 
362 {
363  if (!m_fileButtonList)
364  return;
365 
367  while (!m_fileData.isEmpty())
368  delete m_fileData.takeFirst();
369  m_fileData.clear();
370 
371  QDir d;
372 
373  d.setPath(m_curDirectory);
374  if (d.exists())
375  {
376  // first get a list of directory's in the current directory
377  QStringList filters;
378  filters << "*";
379  QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);
380 
381  for (const auto & fi : qAsConst(list))
382  {
383  if (fi.fileName() != ".")
384  {
385  auto *data = new FileData;
386  data->selected = false;
387  data->directory = true;
388  data->filename = fi.fileName();
389  data->size = 0;
390  m_fileData.append(data);
391 
392  // add a row to the MythUIButtonList
393  auto* item = new
394  MythUIButtonListItem(m_fileButtonList, data->filename);
395  item->setCheckable(false);
396  item->SetImage("ma_folder.png");
397  item->SetData(QVariant::fromValue(data));
398  }
399  }
400 
402  {
403  // second get a list of file's in the current directory
404  filters.clear();
405 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
406  filters = m_filemask.split(" ", QString::SkipEmptyParts);
407 #else
408  filters = m_filemask.split(" ", Qt::SkipEmptyParts);
409 #endif
410  list = d.entryInfoList(filters, QDir::Files, QDir::Name);
411  for (const auto & fi : qAsConst(list))
412  {
413  auto *data = new FileData;
414  data->selected = false;
415  data->directory = false;
416  data->filename = fi.fileName();
417  data->size = fi.size();
418  m_fileData.append(data);
419 
420  // add a row to the UIListBtnArea
421  auto* item =
422  new MythUIButtonListItem(m_fileButtonList, data->filename);
423  item->SetText(formatSize(data->size / 1024, 2), "size");
424 
426  {
427  item->setCheckable(true);
428 
429  QString fullPath = m_curDirectory;
430  if (!fullPath.endsWith("/"))
431  fullPath += "/";
432  fullPath += data->filename;
433 
434  if (m_selectedList.indexOf(fullPath) != -1)
435  {
436  item->setChecked(MythUIButtonListItem::FullChecked);
437  }
438  else
439  {
440  item->setChecked(MythUIButtonListItem::NotChecked);
441  }
442  }
443  else
444  item->setCheckable(false);
445 
446  item->SetData(QVariant::fromValue(data));
447  }
448  }
450  }
451  else
452  {
453  m_locationEdit->SetText("/");
454  LOG(VB_GENERAL, LOG_ERR,
455  "MythArchive: current directory does not exist!");
456  }
457 }
MythUIButton::Clicked
void Clicked()
FSTYPE_FILE
@ FSTYPE_FILE
Definition: fileselector.h:29
FileData::selected
bool selected
Definition: fileselector.h:21
FileSelector::backPressed
void backPressed(void)
Definition: fileselector.cpp:188
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
mythuitext.h
FileData
Definition: fileselector.h:18
fileselector.h
FileSelector::itemClicked
void itemClicked(MythUIButtonListItem *item)
Definition: fileselector.cpp:125
mythdb.h
FileSelector::Create
bool Create(void) override
Definition: fileselector.cpp:37
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
FileSelector::updateFileList
void updateFileList(void)
Definition: fileselector.cpp:361
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:636
FileSelector::homePressed
void homePressed(void)
Definition: fileselector.cpp:200
mythdialogbox.h
FileSelector::keyPressEvent
bool keyPressEvent(QKeyEvent *e) override
Key event handler.
Definition: fileselector.cpp:98
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:177
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:27
MythUITextEdit::SetText
void SetText(const QString &text, bool moveCursor=true)
Definition: mythuitextedit.cpp:216
ArchiveItem::type
QString type
Definition: archiveutil.h:53
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1114
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:182
f
QTextStream t & f
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:603
FileSelector::m_locationEdit
MythUITextEdit * m_locationEdit
Definition: fileselector.h:88
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3580
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:207
mythuihelper.h
FileSelector::~FileSelector
~FileSelector() override
Definition: fileselector.cpp:31
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:133
MythUIButtonListItem::state
CheckState state() const
Definition: mythuibuttonlist.cpp:3534
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:104
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:22
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
ArchiveItem
Definition: archiveutil.h:50
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:3544
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:557
FileSelector::m_cancelButton
MythUIButton * m_cancelButton
Definition: fileselector.h:90
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:32