AstRawDark
Manage your astrophoto dark frames
 All Classes Functions Variables Enumerations Enumerator Pages
prefDialog.cpp
1 /*
2  * AstRawDark
3  *
4  * Copyright (C) 2015 - Frédéric CORNU
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) 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  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "prefDialog.h"
21 #include "ui_prefDialog.h"
22 #include "data/dataStore.h"
23 #include "commands/signalDispatcher.h"
24 #include "globals.h"
25 
26 #include <QFileDialog>
27 #include <QDir>
28 #include <QMessageBox>
29 #include <QListWidgetItem>
30 
31 #ifndef QT_NO_DEBUG
32 #include <QDebug>
33 #endif
34 
35 PrefDialog::PrefDialog(QWidget *parent) :
36  QDialog(parent),
37  ui(new Ui::PrefDialog)
38 {
39  ui->setupUi(this);
40 
41  ui->lstDarkFolders->addItems(DataStore::getInstance()->getDarkSources());
42  ui->lstDarkFolders->clearSelection();
43 
44  ui->chkRemeberWindowGeometry->setChecked(DataStore::getInstance()->getRememberWindowGeometry());
45  ui->chkDarkScanOnStartup->setChecked(DataStore::getInstance()->getScanDarkOnStartup());
46 
47  connect(this,
51 
52  connect(this,
56 }
57 
59 {
60  delete ui;
61 }
62 
63 void PrefDialog::changeEvent(QEvent *e)
64 {
66  switch (e->type()) {
67  case QEvent::LanguageChange:
68  ui->retranslateUi(this);
69  break;
70  default:
71  break;
72  }
73 }
74 
75 bool PrefDialog::isDarkSourceValid(const QString &path, const int row)
76 {
77  {
78  // check if folder is a child, parent or duplicate of any other source folder
79  QDir candidate(path);
80 
81  for (int i=0; i<ui->lstDarkFolders->count(); i++) {
82 
83  QDir test(ui->lstDarkFolders->item(i)->text());
84 
85  if ( row != i && test.exists() ) {
86 
87  if ( test.canonicalPath().startsWith(candidate.canonicalPath()) ||
88  candidate.canonicalPath().startsWith(test.canonicalPath()) ||
89  test.absolutePath().startsWith(candidate.absolutePath()) ||
90  candidate.absolutePath().startsWith(test.absolutePath()) ) {
91 
92  QString warningMsg = QString("<h3>").append(tr("Folder '"))
93  .append(path)
94  .append(tr("' cannot be used as a dark source."))
95  .append("</h3>")
96  .append(tr("It is either a child, a parent or a duplicate"))
97  .append(tr(" of one of your existing sources."));
98 
99  QMessageBox::critical(this,
100  tr("Cannot add this folder as dark source"),
101  warningMsg);
102 
103  return false;
104 
105  }
106  }
107  }
108 
109  return true;
110  }
111 }
112 
113 void PrefDialog::on_btnAddDarkFolder_clicked()
114 {
115  ui->lstDarkFolders->clearSelection();
116 
117  QString basefolder = QFileDialog::getExistingDirectory(this,
118  tr("Please select dark base folder"),
119  QDir::homePath(),
120  QFileDialog::ShowDirsOnly);
121 
122  if ( ! basefolder.isEmpty() && isDarkSourceValid(basefolder)) {
123 
124  ui->lstDarkFolders->addItem(basefolder);
125  }
126 }
127 
128 void PrefDialog::on_lstDarkFolders_itemSelectionChanged()
129 {
130  /*
131  * if one item is selected in the dark sources list
132  * we activate the 'remove' & 'edit' buttons
133  */
134 
135  ui->btnRemoveDarkFolder->setEnabled( ! ui->lstDarkFolders->selectedItems().empty() );
136  ui->btnEditDarkFolder->setEnabled( ! ui->lstDarkFolders->selectedItems().empty() );
137 }
138 
139 void PrefDialog::on_btnRemoveDarkFolder_clicked()
140 {
141  if ( ! ui->lstDarkFolders->selectedItems().empty() ) {
142 
143  ui->lstDarkFolders->takeItem(ui->lstDarkFolders->currentRow());
144  }
145 
146  ui->lstDarkFolders->clearSelection();
147 }
148 
149 void PrefDialog::on_btnEditDarkFolder_clicked()
150 {
151  if ( ! ui->lstDarkFolders->selectedItems().empty() ) {
152 
153  QString selectedPath = ui->lstDarkFolders->item(ui->lstDarkFolders->currentRow())->text();
154 
155  QString basefolder = QFileDialog::getExistingDirectory(this,
156  tr("Please select dark base folder"),
157  selectedPath,
158  QFileDialog::ShowDirsOnly);
159 
160  if ( ! basefolder.isEmpty() &&
161  isDarkSourceValid(basefolder, ui->lstDarkFolders->currentRow()) ) {
162 
163  ui->lstDarkFolders->item(ui->lstDarkFolders->currentRow())->setText(basefolder);
164  }
165 
166  ui->lstDarkFolders->clearSelection();
167  }
168 }
169 
170 void PrefDialog::on_lstDarkFolders_itemDoubleClicked(QListWidgetItem *item)
171 {
172  if ( item ) {
173 
174  QString basefolder = QFileDialog::getExistingDirectory(this,
175  tr("Please select dark base folder"),
176  item->text(),
177  QFileDialog::ShowDirsOnly);
178 
179  if ( ! basefolder.isEmpty() &&
180  isDarkSourceValid(basefolder, ui->lstDarkFolders->currentRow()) ) {
181 
182  ui->lstDarkFolders->item(ui->lstDarkFolders->currentRow())->setText(basefolder);
183  }
184 
185  ui->lstDarkFolders->clearSelection();
186  }
187 }
188 
189 void PrefDialog::accept()
190 {
191  _darkSources.clear();
192 
193  for ( int i=0; i<ui->lstDarkFolders->count(); i++ ) {
194 
195  _darkSources << ui->lstDarkFolders->item(i)->text();
196  }
197 
198  QStringList oldDarkSources = DataStore::getInstance()->getDarkSources();
199  oldDarkSources.sort();
200  _darkSources.sort();
201 
202  bool samesources = oldDarkSources.size() == _darkSources.size() &&
203  std::equal(oldDarkSources.begin(), oldDarkSources.end(), _darkSources.begin());
204 
205  if ( ! samesources ) {
206 
207  emit newDarkSources(_darkSources);
208 
209  hide();
210 
211  QMessageBox messageBox(QMessageBox::Question,
212  tr("Dark library rescan needed"),
213  QString("<h3>")
214  .append(tr("Your dark frames sources have changed. "))
215  .append("</h3>")
216  .append(tr("Would you like to rescan your library now ?")),
217  QMessageBox::Yes | QMessageBox::No,
218  this);
219  messageBox.setButtonText(QMessageBox::Yes, tr("Yes"));
220  messageBox.setButtonText(QMessageBox::No, tr("No"));
221 
222  if ( QMessageBox::Yes == messageBox.exec() ) {
223 
224  emit createDarkScanCommand();
225  }
226  }
227 
228  DataStore::getInstance()->setRememberWindowGeometry(ui->chkRemeberWindowGeometry->isChecked());
229  DataStore::getInstance()->setScanDarkOnStartup(ui->chkDarkScanOnStartup->isChecked());
230 
231  QDialog::accept();
232 }