Changeset View
Changeset View
Standalone View
Standalone View
src/search/dolphinfacetswidget.cpp
Show All 15 Lines | |||||
16 | * Free Software Foundation, Inc., * | 16 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||
18 | * **************************************************************************/ | 18 | * **************************************************************************/ | ||
19 | 19 | | |||
20 | #include "dolphinfacetswidget.h" | 20 | #include "dolphinfacetswidget.h" | ||
21 | 21 | | |||
22 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
23 | 23 | | |||
24 | #include <QButtonGroup> | 24 | #include <QComboBox> | ||
25 | #include <QCheckBox> | | |||
26 | #include <QDate> | 25 | #include <QDate> | ||
27 | #include <QEvent> | 26 | #include <QEvent> | ||
28 | #include <QHBoxLayout> | 27 | #include <QHBoxLayout> | ||
29 | #include <QRadioButton> | 28 | #include <QIcon> | ||
30 | 29 | | |||
31 | DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) : | 30 | DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) : | ||
32 | QWidget(parent), | 31 | QWidget(parent), | ||
33 | m_folders(nullptr), | 32 | m_typeSelector(nullptr), | ||
34 | m_documents(nullptr), | 33 | m_dateSelector(nullptr), | ||
35 | m_images(nullptr), | 34 | m_ratingSelector(nullptr) | ||
36 | m_audio(nullptr), | 35 | { | ||
37 | m_videos(nullptr), | 36 | m_typeSelector = new QComboBox(this); | ||
38 | m_anytime(nullptr), | 37 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("none")), i18nc("@item:inlistbox", "Any Type"), QString()); | ||
39 | m_today(nullptr), | 38 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("inode-directory")), i18nc("@item:inlistbox", "Folders") , QStringLiteral("Folder")); | ||
40 | m_yesterday(nullptr), | 39 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("text-x-generic")), i18nc("@item:inlistbox", "Documents") , QStringLiteral("Document")); | ||
41 | m_thisWeek(nullptr), | 40 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("image-x-generic")), i18nc("@item:inlistbox", "Images") , QStringLiteral("Image")); | ||
42 | m_thisMonth(nullptr), | 41 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("audio-x-generic")), i18nc("@item:inlistbox", "Audio Files"), QStringLiteral("Audio")); | ||
43 | m_thisYear(nullptr), | 42 | m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("video-x-generic")), i18nc("@item:inlistbox", "Videos") , QStringLiteral("Video")); | ||
44 | m_anyRating(nullptr), | 43 | initComboBox(m_typeSelector); | ||
45 | m_oneOrMore(nullptr), | 44 | | ||
46 | m_twoOrMore(nullptr), | 45 | const QDate currentDate = QDate::currentDate(); | ||
47 | m_threeOrMore(nullptr), | 46 | | ||
48 | m_fourOrMore(nullptr), | 47 | m_dateSelector = new QComboBox(this); | ||
49 | m_maxRating(nullptr) | 48 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar")), i18nc("@item:inlistbox", "Any Date"), QDate()); | ||
50 | { | 49 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Today") , currentDate); | ||
51 | QButtonGroup* filetypeGroup = new QButtonGroup(this); | 50 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Yesterday") , currentDate.addDays(-1)); | ||
52 | m_anyType = createRadioButton(i18nc("@option:check", "Any"), filetypeGroup); | 51 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-week")), i18nc("@item:inlistbox", "This Week") , currentDate.addDays(1 - currentDate.dayOfWeek())); | ||
53 | m_folders = createRadioButton(i18nc("@option:check", "Folders"), filetypeGroup); | 52 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-month")), i18nc("@item:inlistbox", "This Month"), currentDate.addDays(1 - currentDate.day())); | ||
54 | m_documents = createRadioButton(i18nc("@option:check", "Documents"), filetypeGroup); | 53 | m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-year")), i18nc("@item:inlistbox", "This Year") , currentDate.addDays(1 - currentDate.dayOfYear())); | ||
55 | m_images = createRadioButton(i18nc("@option:check", "Images"), filetypeGroup); | 54 | initComboBox(m_dateSelector); | ||
56 | m_audio = createRadioButton(i18nc("@option:check", "Audio Files"), filetypeGroup); | 55 | | ||
57 | m_videos = createRadioButton(i18nc("@option:check", "Videos"), filetypeGroup); | 56 | m_ratingSelector = new QComboBox(this); | ||
58 | 57 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("non-starred-symbolic")), i18nc("@item:inlistbox", "Any Rating"), 0); | |||
59 | QVBoxLayout* typeLayout = new QVBoxLayout(); | 58 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "1 or more"), 1); | ||
60 | typeLayout->setSpacing(0); | 59 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "2 or more"), 2); | ||
61 | typeLayout->addWidget(m_anyType); | 60 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "3 or more"), 3); | ||
62 | typeLayout->addWidget(m_folders); | 61 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "4 or more"), 4); | ||
63 | typeLayout->addWidget(m_documents); | 62 | m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "Highest Rating"), 5); | ||
64 | typeLayout->addWidget(m_images); | 63 | initComboBox(m_ratingSelector); | ||
65 | typeLayout->addWidget(m_audio); | | |||
66 | typeLayout->addWidget(m_videos); | | |||
67 | typeLayout->addStretch(); | | |||
68 | | ||||
69 | QButtonGroup* timespanGroup = new QButtonGroup(this); | | |||
70 | m_anytime = createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup); | | |||
71 | m_today = createRadioButton(i18nc("@option:option", "Today"), timespanGroup); | | |||
72 | m_yesterday = createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup); | | |||
73 | m_thisWeek = createRadioButton(i18nc("@option:option", "This Week"), timespanGroup); | | |||
74 | m_thisMonth = createRadioButton(i18nc("@option:option", "This Month"), timespanGroup); | | |||
75 | m_thisYear = createRadioButton(i18nc("@option:option", "This Year"), timespanGroup); | | |||
76 | | ||||
77 | QVBoxLayout* timespanLayout = new QVBoxLayout(); | | |||
78 | timespanLayout->setSpacing(0); | | |||
79 | timespanLayout->addWidget(m_anytime); | | |||
80 | timespanLayout->addWidget(m_today); | | |||
81 | timespanLayout->addWidget(m_yesterday); | | |||
82 | timespanLayout->addWidget(m_thisWeek); | | |||
83 | timespanLayout->addWidget(m_thisMonth); | | |||
84 | timespanLayout->addWidget(m_thisYear); | | |||
85 | timespanLayout->addStretch(); | | |||
86 | | ||||
87 | QButtonGroup* ratingGroup = new QButtonGroup(this); | | |||
88 | m_anyRating = createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup); | | |||
89 | m_oneOrMore = createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup); | | |||
90 | m_twoOrMore = createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup); | | |||
91 | m_threeOrMore = createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup); | | |||
92 | m_fourOrMore = createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup); | | |||
93 | m_maxRating = createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup); | | |||
94 | | ||||
95 | QVBoxLayout* ratingLayout = new QVBoxLayout(); | | |||
96 | ratingLayout->setSpacing(0); | | |||
97 | ratingLayout->addWidget(m_anyRating); | | |||
98 | ratingLayout->addWidget(m_oneOrMore); | | |||
99 | ratingLayout->addWidget(m_twoOrMore); | | |||
100 | ratingLayout->addWidget(m_threeOrMore); | | |||
101 | ratingLayout->addWidget(m_fourOrMore); | | |||
102 | ratingLayout->addWidget(m_maxRating); | | |||
103 | 64 | | |||
104 | QHBoxLayout* topLayout = new QHBoxLayout(this); | 65 | QHBoxLayout* topLayout = new QHBoxLayout(this); | ||
105 | topLayout->addLayout(typeLayout); | 66 | topLayout->addWidget(m_typeSelector); | ||
106 | topLayout->addLayout(timespanLayout); | 67 | topLayout->addWidget(m_dateSelector); | ||
107 | topLayout->addLayout(ratingLayout); | 68 | topLayout->addWidget(m_ratingSelector); | ||
108 | topLayout->addStretch(); | | |||
109 | 69 | | |||
110 | resetOptions(); | 70 | resetOptions(); | ||
111 | } | 71 | } | ||
112 | 72 | | |||
113 | DolphinFacetsWidget::~DolphinFacetsWidget() | 73 | DolphinFacetsWidget::~DolphinFacetsWidget() | ||
114 | { | 74 | { | ||
115 | } | 75 | } | ||
116 | 76 | | |||
Show All 10 Lines | 85 | { | |||
127 | m_anytime->setChecked(true); | 87 | m_anytime->setChecked(true); | ||
128 | m_anyRating->setChecked(true); | 88 | m_anyRating->setChecked(true); | ||
129 | } | 89 | } | ||
130 | 90 | | |||
131 | QString DolphinFacetsWidget::ratingTerm() const | 91 | QString DolphinFacetsWidget::ratingTerm() const | ||
132 | { | 92 | { | ||
133 | QStringList terms; | 93 | QStringList terms; | ||
134 | 94 | | |||
135 | if (!m_anyRating->isChecked()) { | 95 | if (m_ratingSelector->currentIndex() > 0) { | ||
136 | int stars = 1; // represents m_oneOrMore | 96 | const int rating = m_ratingSelector->currentData().toInt() * 2; | ||
137 | if (m_twoOrMore->isChecked()) { | | |||
138 | stars = 2; | | |||
139 | } else if (m_threeOrMore->isChecked()) { | | |||
140 | stars = 3; | | |||
141 | } else if (m_fourOrMore->isChecked()) { | | |||
142 | stars = 4; | | |||
143 | } else if (m_maxRating->isChecked()) { | | |||
144 | stars = 5; | | |||
145 | } | | |||
146 | | ||||
147 | const int rating = stars * 2; | | |||
148 | terms << QStringLiteral("rating>=%1").arg(rating); | 97 | terms << QStringLiteral("rating>=%1").arg(rating); | ||
149 | } | 98 | } | ||
150 | 99 | | |||
151 | if (!m_anytime->isChecked()) { | 100 | if (m_dateSelector->currentIndex() > 0) { | ||
152 | QDate date = QDate::currentDate(); // represents m_today | 101 | const QDate date = m_dateSelector->currentData().toDate(); | ||
153 | if (m_yesterday->isChecked()) { | | |||
154 | date = date.addDays(-1); | | |||
155 | } else if (m_thisWeek->isChecked()) { | | |||
156 | date = date.addDays(1 - date.dayOfWeek()); | | |||
157 | } else if (m_thisMonth->isChecked()) { | | |||
158 | date = date.addDays(1 - date.day()); | | |||
159 | } else if (m_thisYear->isChecked()) { | | |||
160 | date = date.addDays(1 - date.dayOfYear()); | | |||
161 | } | | |||
162 | | ||||
163 | terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate)); | 102 | terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate)); | ||
164 | } | 103 | } | ||
165 | 104 | | |||
166 | return terms.join(QLatin1String(" AND ")); | 105 | return terms.join(QLatin1String(" AND ")); | ||
167 | } | 106 | } | ||
168 | 107 | | |||
169 | QString DolphinFacetsWidget::facetType() const | 108 | QString DolphinFacetsWidget::facetType() const | ||
170 | { | 109 | { | ||
171 | if (m_folders->isChecked()) { | 110 | return m_typeSelector->currentData().toString(); | ||
172 | return QStringLiteral("Folder"); | | |||
173 | } else if (m_documents->isChecked()) { | | |||
174 | return QStringLiteral("Document"); | | |||
175 | } else if (m_images->isChecked()) { | | |||
176 | return QStringLiteral("Image"); | | |||
177 | } else if (m_audio->isChecked()) { | | |||
178 | return QStringLiteral("Audio"); | | |||
179 | } else if (m_videos->isChecked()) { | | |||
180 | return QStringLiteral("Video"); | | |||
181 | } | | |||
182 | | ||||
183 | return QString(); | | |||
184 | } | 111 | } | ||
185 | 112 | | |||
186 | bool DolphinFacetsWidget::isRatingTerm(const QString& term) const | 113 | bool DolphinFacetsWidget::isRatingTerm(const QString& term) const | ||
187 | { | 114 | { | ||
188 | const QStringList subTerms = term.split(' ', QString::SkipEmptyParts); | 115 | const QStringList subTerms = term.split(' ', QString::SkipEmptyParts); | ||
189 | 116 | | |||
190 | // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms. | 117 | // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms. | ||
191 | bool containsRating = false; | 118 | bool containsRating = false; | ||
192 | bool containsModified = false; | 119 | bool containsModified = false; | ||
193 | 120 | | |||
194 | foreach (const QString& subTerm, subTerms) { | 121 | for (const QString& subTerm : subTerms) { | ||
elvisangelaccio: Unrelated change, please move it to another commit. | |||||
195 | if (subTerm.startsWith(QLatin1String("rating>="))) { | 122 | if (subTerm.startsWith(QLatin1String("rating>="))) { | ||
196 | containsRating = true; | 123 | containsRating = true; | ||
197 | } else if (subTerm.startsWith(QLatin1String("modified>="))) { | 124 | } else if (subTerm.startsWith(QLatin1String("modified>="))) { | ||
198 | containsModified = true; | 125 | containsModified = true; | ||
199 | } | 126 | } | ||
200 | } | 127 | } | ||
201 | 128 | | |||
202 | return containsModified || containsRating; | 129 | return containsModified || containsRating; | ||
203 | } | 130 | } | ||
204 | 131 | | |||
205 | void DolphinFacetsWidget::setRatingTerm(const QString& term) | 132 | void DolphinFacetsWidget::setRatingTerm(const QString& term) | ||
206 | { | 133 | { | ||
207 | // If term has sub terms, then the sub terms are always "rating" and "modified" terms. | 134 | // If term has sub terms, then the sub terms are always "rating" and "modified" terms. | ||
208 | // If term has no sub terms, then the term itself is either a "rating" term or a "modified" | 135 | // If term has no sub terms, then the term itself is either a "rating" term or a "modified" | ||
209 | // term. To avoid code duplication we add term to subTerms list, if the list is empty. | 136 | // term. To avoid code duplication we add term to subTerms list, if the list is empty. | ||
210 | QStringList subTerms = term.split(' ', QString::SkipEmptyParts); | 137 | QStringList subTerms = term.split(' ', QString::SkipEmptyParts); | ||
211 | 138 | | |||
212 | foreach (const QString& subTerm, subTerms) { | 139 | for (const QString& subTerm : subTerms) { | ||
Unrelated change, please move it to another commit. (and subTerms should be made const to avoid a detach). elvisangelaccio: Unrelated change, please move it to another commit.
(and `subTerms` should be made `const` to… | |||||
213 | if (subTerm.startsWith(QLatin1String("modified>="))) { | 140 | if (subTerm.startsWith(QLatin1String("modified>="))) { | ||
214 | const QString value = subTerm.mid(10); | 141 | const QString value = subTerm.mid(10); | ||
215 | const QDate date = QDate::fromString(value, Qt::ISODate); | 142 | const QDate date = QDate::fromString(value, Qt::ISODate); | ||
216 | setTimespan(date); | 143 | setTimespan(date); | ||
217 | } else if (subTerm.startsWith(QLatin1String("rating>="))) { | 144 | } else if (subTerm.startsWith(QLatin1String("rating>="))) { | ||
218 | const QString value = subTerm.mid(8); | 145 | const QString value = subTerm.mid(8); | ||
219 | const int stars = value.toInt() / 2; | 146 | const int stars = value.toInt() / 2; | ||
220 | setRating(stars); | 147 | setRating(stars); | ||
221 | } | 148 | } | ||
222 | } | 149 | } | ||
223 | } | 150 | } | ||
224 | 151 | | |||
225 | void DolphinFacetsWidget::setFacetType(const QString& type) | 152 | void DolphinFacetsWidget::setFacetType(const QString& type) | ||
226 | { | 153 | { | ||
227 | if (type == QLatin1String("Folder")) { | 154 | for ( int index = 1; index <= m_typeSelector->count(); index++ ) { | ||
elvisangelaccio: Coding style: no space after/before () | |||||
228 | m_folders->setChecked(true); | 155 | if (type == m_typeSelector->itemData(index).toString()) { | ||
229 | } else if (type == QLatin1String("Document")) { | 156 | m_typeSelector->setCurrentIndex(index); | ||
230 | m_documents->setChecked(true); | 157 | break; | ||
231 | } else if (type == QLatin1String("Image")) { | 158 | } | ||
232 | m_images->setChecked(true); | | |||
233 | } else if (type == QLatin1String("Audio")) { | | |||
234 | m_audio->setChecked(true); | | |||
235 | } else if (type == QLatin1String("Video")) { | | |||
236 | m_videos->setChecked(true); | | |||
237 | } else { | | |||
238 | m_anyType->setChecked(true); | | |||
239 | } | 159 | } | ||
240 | } | 160 | } | ||
241 | 161 | | |||
242 | void DolphinFacetsWidget::setRating(const int stars) | 162 | void DolphinFacetsWidget::setRating(const int stars) | ||
243 | { | 163 | { | ||
244 | switch (stars) { | 164 | if (stars<0 || stars>5) { | ||
elvisangelaccio: Coding style: add space before/after comparison operators. | |||||
245 | case 5: | 165 | return; | ||
246 | m_maxRating->setChecked(true); | | |||
247 | break; | | |||
248 | | ||||
249 | case 4: | | |||
250 | m_fourOrMore->setChecked(true); | | |||
251 | break; | | |||
252 | | ||||
253 | case 3: | | |||
254 | m_threeOrMore->setChecked(true); | | |||
255 | break; | | |||
256 | | ||||
257 | case 2: | | |||
258 | m_twoOrMore->setChecked(true); | | |||
259 | break; | | |||
260 | | ||||
261 | case 1: | | |||
262 | m_oneOrMore->setChecked(true); | | |||
263 | break; | | |||
264 | | ||||
265 | default: | | |||
266 | m_anyRating->setChecked(true); | | |||
267 | } | 166 | } | ||
167 | m_ratingSelector->setCurrentIndex(stars); | ||||
268 | } | 168 | } | ||
269 | 169 | | |||
270 | void DolphinFacetsWidget::setTimespan(const QDate& date) | 170 | void DolphinFacetsWidget::setTimespan(const QDate& date) | ||
271 | { | 171 | { | ||
272 | const QDate currentDate = QDate::currentDate(); | 172 | if (!date.isValid()) { | ||
273 | const int days = date.daysTo(currentDate); | 173 | return; | ||
274 | 174 | } | |||
275 | if (days <= 0) { | 175 | m_dateSelector->setCurrentIndex(0); | ||
276 | m_today->setChecked(true); | 176 | for ( int index = 1; index <= m_dateSelector->count(); index++ ) { | ||
elvisangelaccio: Unrelated change, please move it to another commit. | |||||
277 | } else if (days <= 1) { | 177 | if (date >= m_dateSelector->itemData(index).toDate()) { | ||
278 | m_yesterday->setChecked(true); | 178 | m_dateSelector->setCurrentIndex(index); | ||
279 | } else if (days <= currentDate.dayOfWeek()) { | 179 | break; | ||
280 | m_thisWeek->setChecked(true); | 180 | } | ||
281 | } else if (days <= currentDate.day()) { | | |||
282 | m_thisMonth->setChecked(true); | | |||
283 | } else if (days <= currentDate.dayOfYear()) { | | |||
284 | m_thisYear->setChecked(true); | | |||
285 | } else { | | |||
286 | m_anytime->setChecked(true); | | |||
287 | } | 181 | } | ||
288 | } | 182 | } | ||
289 | 183 | | |||
290 | QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text, | 184 | void DolphinFacetsWidget::initComboBox(QComboBox* combo) | ||
291 | QButtonGroup* group) | | |||
292 | { | 185 | { | ||
293 | QRadioButton* button = new QRadioButton(text); | 186 | combo->setFrame(false); | ||
294 | connect(button, &QRadioButton::clicked, this, &DolphinFacetsWidget::facetChanged); | 187 | combo->setMinimumHeight(parentWidget()->height()); | ||
295 | group->addButton(button); | 188 | combo->setCurrentIndex(0); | ||
296 | return button; | 189 | connect(combo, QOverload<int>::of(&QComboBox::activated), this, &DolphinFacetsWidget::facetChanged); | ||
297 | } | 190 | } | ||
298 | 191 | |
Unrelated change, please move it to another commit.