BiblioteQ

biblioteq_otheroptions.h
1 /*
2 ** Copyright (c) 2006 - present, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from BiblioteQ without specific prior written permission.
15 **
16 ** BIBLIOTEQ IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** BIBLIOTEQ, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef _BIBLIOTEQ_OTHEROPTIONS_H_
29 #define _BIBLIOTEQ_OTHEROPTIONS_H_
30
31 #include <QColorDialog>
32 #include <QLineEdit>
33 #include <QSettings>
34 #include <QStyledItemDelegate>
35 #include <QTimer>
36
37 #include "ui_biblioteq_otheroptions.h"
38
39 class biblioteq_otheroptions_shortcut_lineedit: public QLineEdit
40 {
41 Q_OBJECT
42
43 public:
44 biblioteq_otheroptions_shortcut_lineedit(QWidget *parent):QLineEdit(parent)
45 {
46 }
47
48 private:
49 void keyPressEvent(QKeyEvent *event)
50 {
51 QLineEdit::keyReleaseEvent(event);
52
53 if(event)
54 {
55 auto key = static_cast<Qt::Key> (event->key());
56
57 if(key == Qt::Key_Alt ||
58 key == Qt::Key_Control ||
59 key == Qt::Key_Meta ||
60 key == Qt::Key_Shift)
61 return;
62
63 auto integer = event->key();
64 auto modifiers = event->modifiers();
65
66 if(modifiers & Qt::AltModifier)
67 integer += Qt::ALT;
68
69 if(modifiers & Qt::ControlModifier)
70 integer += Qt::CTRL;
71
72 if(modifiers & Qt::MetaModifier)
73 integer += Qt::META;
74
75 if(modifiers & Qt::ShiftModifier)
76 integer += Qt::SHIFT;
77
78 setText(QKeySequence(integer).toString(QKeySequence::NativeText));
79 }
80 }
81 };
82
83 class biblioteq_otheroptions_item_delegate: public QStyledItemDelegate
84 {
85 Q_OBJECT
86
87 public:
88 enum class ParentTypes
89 {
90 Keywords = 0,
91 Shortcuts
92 };
93
94 biblioteq_otheroptions_item_delegate
95 (const ParentTypes type, QObject *parent):QStyledItemDelegate(parent)
96 {
97 m_type = type;
98 }
99
100 QWidget *createEditor(QWidget *parent,
101 const QStyleOptionViewItem &option,
102 const QModelIndex &index) const
103 {
104 switch(m_type)
105 {
106 case ParentTypes::Keywords:
107 {
108 switch(index.column())
109 {
110 case 1:
111 {
112 auto pushButton = new QPushButton(parent);
113
114 connect(pushButton,
115 SIGNAL(clicked(void)),
116 this,
117 SLOT(slotSelectColor(void))
118 #ifdef Q_OS_MACOS
119 , Qt::QueuedConnection
120 #endif
121 );
122 m_index = index;
123 pushButton->setText(index.data().toString().trimmed());
124 return pushButton;
125 }
126 default:
127 {
128 break;
129 }
130 }
131
132 break;
133 }
134 case ParentTypes::Shortcuts:
135 {
136 switch(index.column())
137 {
138 case 1:
139 {
140 auto lineEdit = new biblioteq_otheroptions_shortcut_lineedit
141 (parent);
142
143 lineEdit->setClearButtonEnabled(true);
144 lineEdit->setPlaceholderText(tr("Shortcut"));
145 lineEdit->setText(index.data().toString().trimmed());
146 return lineEdit;
147 }
148 default:
149 {
150 break;
151 }
152 }
153
154 break;
155 }
156 default:
157 {
158 break;
159 }
160 }
161
162 return QStyledItemDelegate::createEditor(parent, option, index);
163 }
164
165 private:
166 void setModelData(QWidget *editor,
167 QAbstractItemModel *model,
168 const QModelIndex &index) const
169 {
170 auto pushButton = qobject_cast<QPushButton *> (editor);
171
172 if(model && pushButton)
173 {
174 pushButton->setText(model->data(index).toString().trimmed());
175 return;
176 }
177
178 QStyledItemDelegate::setModelData(editor, model, index);
179 QTimer::singleShot(1000, this, SIGNAL(changed(void))); // Constness.
180 }
181
182 ParentTypes m_type;
183 mutable QModelIndex m_index;
184
185 private slots:
186 void slotSelectColor(void)
187 {
188 auto pushButton = qobject_cast<QPushButton *> (sender());
189
190 if(!pushButton)
191 return;
192
193 QColorDialog dialog(pushButton);
194
195 dialog.setCurrentColor(QColor(pushButton->text().remove('&')));
196
197 if(dialog.exec() == QDialog::Accepted)
198 {
199 if(m_index.isValid() && m_index.model())
200 {
201 auto sortingEnabled = false;
202 auto table = qobject_cast<QTableWidget *>
203 (m_index.model()->parent());
204
205 if(table)
206 {
207 sortingEnabled = table->isSortingEnabled();
208 table->setSortingEnabled(false);
209 }
210
211 const_cast<QAbstractItemModel *> (m_index.model())->setData
212 (m_index, dialog.selectedColor(), Qt::DecorationRole);
213 const_cast<QAbstractItemModel *> (m_index.model())->setData
214 (m_index, dialog.selectedColor().name());
215
216 if(table)
217 table->setSortingEnabled(sortingEnabled);
218 }
219
220 pushButton->setText(dialog.selectedColor().name());
221 emit commitData(pushButton);
222 emit closeEditor(pushButton); // Order is crucial.
223 }
224 }
225
226 signals:
227 void changed(void);
228 };
229
230 class biblioteq;
231
232 class biblioteq_otheroptions: public QMainWindow
233 {
234 Q_OBJECT
235
236 public:
237 biblioteq_otheroptions(biblioteq *parent);
238 ~biblioteq_otheroptions();
239 QColor availabilityColor(const QString &it) const;
240 QColor itemMandatoryFieldColor(void) const;
241 QColor itemQueryResultColor(void) const;
242 QMap<QString, QColor> customQueryColors(void) const;
243
244 QString isbn10DisplayFormat(const QString &str) const
245 {
246 if(QString(str).remove('-').length() != 10)
247 return str;
248
249 if(m_isbn10Format.isEmpty())
250 {
251 QSettings settings;
252 auto index = qBound
253 (0,
254 settings.value("otheroptions/isbn10_display_format_index").toInt(),
255 m_ui.isbn10_display_format->count() - 1);
256
257 m_isbn10Format = m_ui.isbn10_display_format->itemText(index);
258 }
259
260 return isbnDisplayFormat(m_isbn10Format, str);
261 }
262
263 QString isbn13DisplayFormat(const QString &str) const
264 {
265 if(QString(str).remove('-').length() != 13)
266 return str;
267
268 if(m_isbn13Format.isEmpty())
269 {
270 QSettings settings;
271 auto index = qBound
272 (0,
273 settings.value("otheroptions/isbn13_display_format_index").toInt(),
274 m_ui.isbn13_display_format->count() - 1);
275
276 m_isbn13Format = m_ui.isbn13_display_format->itemText(index);
277 }
278
279 return isbnDisplayFormat(m_isbn13Format, str);
280 }
281
282 QString publicationDateFormat(const QString &it) const;
283 bool isMembersColumnVisible(const QString &text) const;
284 bool showBookReadStatus(void) const;
285 bool showMainTableImages(void) const;
286 bool showMainTableProgressDialogs(void) const;
287 int booksAccessionNumberIndex(void) const;
288 int iconsViewColumnCount(void) const;
289 void prepareSettings(void);
290 void prepareShortcuts(void);
291 void prepareMembersVisibleColumns(QTableWidget *table);
292
293 private:
294 enum ItemsColumns
295 {
296 AVAILABILITY_COLOR = 1,
297 ITEM_TYPE = 0,
298 PUBLICATION_DATE_FORMAT = 1
299 };
300
301 Ui_otheroptions m_ui;
302 biblioteq *qmain;
303 biblioteq_otheroptions_item_delegate *m_keywordsItemDelegate;
304 biblioteq_otheroptions_item_delegate *m_shortcutsItemDelegate;
305 mutable QString m_isbn10Format;
306 mutable QString m_isbn13Format;
307
308 QString isbnDisplayFormat(const QString &format, const QString &s) const
309 {
310 auto str(QString(s).remove('-'));
311
312 if(!(str.length() == 10 || str.length() == 13))
313 return s;
314
315 QString text("");
316 auto list(format.split('-'));
317
318 for(int i = 0, j = 0; i < list.size(); i++)
319 {
320 text.append(str.mid(j, list.at(i).length()));
321
322 if(i != list.size() - 1)
323 text.append('-');
324
325 j += list.at(i).length();
326 }
327
328 return text;
329 }
330
331 void changeEvent(QEvent *event);
332 void keyPressEvent(QKeyEvent *event);
333 void prepareAvailability(void);
334 void prepareSQLKeywords(void);
335
336 private slots:
337 void setGlobalFonts(const QFont &font);
338 void slotClose(void);
339 void slotMainWindowShortcutChanged(void);
340 void slotPreviewCanvasBackgroundColor(const QColor &color);
341 void slotResetCustomQueryColors(void);
342 void slotSave(void);
343 void slotSelectAvailabilityColor(void);
344 void slotSelectColor(void);
345 void slotSelectMainwindowCanvasBackgroundColor(void);
346
347 public slots:
348 void showNormal(void);
349
350 signals:
351 void mainWindowCanvasBackgroundColorChanged(const QColor &color);
352 void mainWindowCanvasBackgroundColorPreview(const QColor &color);
353 void saved(void);
354 };
355
356 #endif
biblioteq_otheroptions_item_delegate
Definition: biblioteq_otheroptions.h:84
biblioteq_otheroptions_shortcut_lineedit
Definition: biblioteq_otheroptions.h:40
biblioteq_otheroptions
Definition: biblioteq_otheroptions.h:233
biblioteq
Definition: biblioteq.h:274