MythTV  master
galleryfilterdlg.cpp
Go to the documentation of this file.
1 /* ============================================================
2  * File : galleryfilterdlg.cpp
3  * Description :
4  *
5  * This program is free software; you can redistribute it
6  * and/or modify it under the terms of the GNU General
7  * Public License as published bythe Free Software Foundation;
8  * either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * ============================================================ */
17 
18 // Own header
19 #include "galleryfilterdlg.h"
20 
21 // Qt headers
22 #include <QApplication>
23 #include <QEvent>
24 #include <QList>
25 
26 // MythTV headers
27 #include <mythdate.h>
28 #include <mythdbcon.h>
29 #include <mythuibuttonlist.h>
30 #include <mythcontext.h>
31 #include <mythlogging.h>
32 #include <mthread.h>
33 
34 #include <unistd.h> // for usleep()
35 
36 // MythGallery headers
37 #include "galleryfilter.h"
38 
39 #define LOC QString("GalleryFilterDlg:")
40 
41 class FilterScanThread : public MThread
42 {
43  public:
44  FilterScanThread(const QString& dir, const GalleryFilter& flt,
45  int *dirCount, int *imageCount, int *movieCount)
46  : MThread("FilterScan"), m_filter(flt), m_dir(dir),
47  m_dirCount(dirCount), m_imgCount(imageCount),
48  m_movCount(movieCount) {}
49  void run() override; // MThread
50 
51  private:
53  QString m_dir;
54  int *m_dirCount {nullptr};
55  int *m_imgCount {nullptr};
56  int *m_movCount {nullptr};
57 };
58 
60 {
61  RunProlog();
62 
64  m_movCount);
65 
66  RunEpilog();
67 }
68 
70  GalleryFilter *filter)
71  : MythScreenType(parent, name)
72 {
73  m_settingsOriginal = filter;
74  m_settingsOriginal->dumpFilter("GalleryFilterDialog:ctor (original)");
76  *m_settingsTemp = *filter;
77  m_settingsTemp->dumpFilter("GalleryFilterDialog:ctor (temporary)");
78  m_photoDir = gCoreContext->GetSetting("GalleryDir", "");
79 }
80 
82 {
83  delete m_settingsTemp;
84 }
85 
87 {
88  if (!LoadWindowFromXML("gallery-ui.xml", "filter", this))
89  return false;
90 
91  bool err = false;
92 
93  UIUtilE::Assign(this, m_dirFilter, "dirfilter_text", &err);
94  UIUtilE::Assign(this, m_typeFilter, "typefilter_select", &err);
95  UIUtilE::Assign(this, m_sortList, "sort_select", &err);
96  UIUtilE::Assign(this, m_checkButton, "check_button", &err);
97  UIUtilE::Assign(this, m_doneButton, "done_button", &err);
98  UIUtilE::Assign(this, m_saveButton, "save_button", &err);
99  UIUtilE::Assign(this, m_numImagesText, "numimages_text", &err);
100 
101  if (err)
102  {
103  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'filter'");
104  return false;
105  }
106 
107  BuildFocusList();
108 
109  fillWidgets();
110 
111  connect(m_dirFilter, SIGNAL(valueChanged()), SLOT(setDirFilter()));
112  connect(m_typeFilter, SIGNAL(itemSelected(MythUIButtonListItem*)),
114  connect(m_sortList, SIGNAL(itemSelected(MythUIButtonListItem*)),
115  SLOT(setSort(MythUIButtonListItem*)));
116  connect(m_checkButton, SIGNAL(Clicked()), SLOT(updateFilter()));
117  connect(m_saveButton, SIGNAL(Clicked()), SLOT(saveAsDefault()));
118  connect(m_doneButton, SIGNAL(Clicked()), SLOT(saveAndExit()));
119 
121 
122  return true;
123 }
124 
126 {
127  // Folder filter
129 
130  // Type Filter
131  new MythUIButtonListItem(m_typeFilter, tr("All"),
133  new MythUIButtonListItem(m_typeFilter, tr("Images only"),
135  new MythUIButtonListItem(m_typeFilter, tr("Movies only"),
138  m_numImagesText->SetText(tr("Filter result : (unknown)"));
139 
140  // Sort order
141  new MythUIButtonListItem(m_sortList, tr("Unsorted"),
143  new MythUIButtonListItem(m_sortList, tr("Name (A-Z alpha)"),
146  tr("Reverse Name (Z-A alpha)"),
148  new MythUIButtonListItem(m_sortList, tr("Mod Time (oldest first)"),
151  tr("Reverse Mod Time (newest first)"),
153  new MythUIButtonListItem(m_sortList, tr("Extension (A-Z alpha)"),
156  tr("Reverse Extension (Z-A alpha)"),
159  tr("Filesize (smallest first)"),
162  tr("Reverse Filesize (largest first)"),
165 }
166 
168 {
169  if (m_scanning)
170  {
171  m_numImagesText->SetText(tr("-- please be patient --"));
172  return;
173  }
174  m_scanning = true;
175 
176  int dir_count = 0;
177  int img_count = 0;
178  int mov_count = 0;
179 
180  m_numImagesText->SetText(tr("-- scanning current filter --"));
181 
182  FilterScanThread fltScan(m_photoDir, *m_settingsTemp, &dir_count,
183  &img_count, &mov_count);
184  fltScan.start();
185 
186  while (!fltScan.isFinished())
187  {
188  usleep(500);
189  qApp->processEvents();
190  }
191 
192  m_scanning = false;
193 
194  if (dir_count + img_count + mov_count == 0)
195  m_numImagesText->SetText(tr("No files / folders found"));
196  else if (dir_count > 0)
197  {
198  if (img_count + mov_count == 0)
200  "Filter result : %1 folder(s) found but no files")
201  .arg(dir_count));
202  else if (img_count == 0)
204  "Filter result : %1 folder(s), %2 movie(s) found")
205  .arg(dir_count).arg(mov_count));
206  else if (mov_count == 0)
208  "Filter result : %1 folder(s), %2 image(s) found")
209  .arg(dir_count).arg(img_count));
210  else
212  "Filter result : %1 folder(s), %2 image(s) and %3 movie(s) "
213  "found").arg(dir_count).arg(img_count).arg(mov_count));
214  }
215  else if (img_count > 0 && mov_count > 0)
217  "Filter result : %1 image(s) and %2 movie(s) found")
218  .arg(img_count).arg(mov_count));
219  else if (mov_count == 0)
221  "Filter result : %1 image(s) found").arg(img_count));
222  else
224  "Filter result : %1 movie(s) found").arg(mov_count));
225 }
226 
228 {
230 }
231 
233 {
234  m_settingsTemp->setTypeFilter(item->GetData().toInt());
235 }
236 
238 {
239  m_settingsTemp->setSort(item->GetData().toInt());
240 }
241 
243 {
244  // Save defaults from temp settings
246  saveAndExit();
247 }
248 
250 {
252 
253  m_settingsOriginal->dumpFilter("GalleryFilterDialog::saveAndExit()");
254 
256  emit filterChanged();
257 
258  Close();
259 }
260 
261 /*
262  * vim:ts=4:sw=4:ai:et:si:sts=4
263  */
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
void dumpFilter(const QString &src)
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void setSort(MythUIButtonListItem *item)
void setTypeFilter(MythUIButtonListItem *item)
void saveAsDefault()
int getTypeFilter() const
Definition: galleryfilter.h:52
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:136
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythUIButtonList * m_typeFilter
GalleryFilter * m_settingsTemp
void BuildFocusList(void)
void setSort(int sort)
Definition: galleryfilter.h:60
void run() override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
virtual void Close()
GalleryFilter m_filter
GalleryFilterDialog(MythScreenStack *parent, const QString &name, GalleryFilter *filter)
MythUITextEdit * m_dirFilter
FilterScanThread(const QString &dir, const GalleryFilter &flt, int *dirCount, int *imageCount, int *movieCount)
QString GetSetting(const QString &key, const QString &defaultval="")
MythUIButton * m_checkButton
MythUIButtonList * m_sortList
MythUIButton * m_doneButton
GalleryFilter * m_settingsOriginal
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
QString getDirFilter() const
Definition: galleryfilter.h:45
const char * name
Definition: ParseText.cpp:328
bool Create() override
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void SetText(const QString &text, bool moveCursor=true)
bool getChangedState()
Definition: galleryfilter.h:66
MythUIText * m_numImagesText
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void setTypeFilter(int typeFilter)
Definition: galleryfilter.h:53
MythUIButton * m_saveButton
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
int getSort() const
Definition: galleryfilter.h:59
bool isFinished(void) const
Definition: mthread.cpp:269
bool SetFocusWidget(MythUIType *widget=nullptr)
Screen in which all other widgets are contained and rendered.
void setDirFilter(QString dirFilter)
Definition: galleryfilter.h:46
static bool TestFilter(const QString &dir, const GalleryFilter &flt, int *dirCount, int *imageCount, int *movieCount)
QString GetText(void) const
void SetValueByData(const QVariant &data)