forked from 0xb8/WiseTagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.h
253 lines (193 loc) · 6.21 KB
/
input.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Copyright © 2014 cat <[email protected]>
* This program is free software. It comes without any warranty, to the extent
* permitted by applicable law. You can redistribute it and/or modify it under
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
* published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
#ifndef TAGINPUT_H
#define TAGINPUT_H
/*! \file input.h
* \brief Class @ref TagInput
*/
#include <QStringList>
#include <QLineEdit>
#include <QStandardItemModel>
#include <memory>
#include "multicompleter.h"
#include "tag_parser.h"
#include "global_enums.h"
class QKeyEvent;
/// Provides a Line Edit widget designed for tagging.
class TagInput : public QLineEdit {
Q_OBJECT
/// Removed tags color.
Q_PROPERTY(QColor removedTagColor READ getRemovedTagColor WRITE setRemovedTagColor DESIGNABLE true)
/// Replaced tags color.
Q_PROPERTY(QColor replacedTagColor READ getReplacedTagColor WRITE setReplacedTagColor DESIGNABLE true)
/// Unknown tags color.
Q_PROPERTY(QColor unknownTagColor READ getUnknownTagColor WRITE setUnknownTagColor DESIGNABLE true)
/// Naming mode line edit background color.
Q_PROPERTY(QColor namingModeBackgroundColor READ getNamingModeBackgroundColor WRITE setNamingModeBackgroundColor DESIGNABLE true)
public:
explicit TagInput(QWidget* _parent = nullptr);
/*!
* \brief Fix and sort tags.
* \param sort Enable tag sorting.
*
* Sorts and removes duplicate tags, makes tags lower-case, adds related
* tags, replaces tags, autoremoves tags.
*
* Related/replaced/autoremoved tags are added only once for each
* candidate tag to allow user to manually undo changes.
*/
void fixTags(bool sort = true);
/*!
* \brief Load and parse tags from \p data.
*/
void loadTagData(const QByteArray& data);
/*!
* \brief Clears loaded tags.
*/
void clearTagData();
/*!
* \brief Reset state used to keep track of already processed tags.
*/
void clearTagEditState();
/*!
* \brief Set initial text.
*
* \param text Initial tags text.
* \param path_length_limit Maximum number of characters allowed for the tag string.
*/
void setText(const QString& text, int path_length_limit);
/*!
* \brief Set current edit mode.
*/
void setEditMode(EditMode mode);
/*!
* \brief Get current edit mode.
*/
EditMode editMode() const noexcept;
/*!
* \brief Set tags while keeping the imageboard id, if present.
*/
void setTags(const QString&);
/*!
* \brief Returns tags without imageboard id, if present.
*/
QString tags() const;
/*!
* \brief Returns list of tags without imageboard id, if present.
*/
QStringList tags_list() const;
/*!
* \brief Returns constant reference to current tag parser.
*/
const TagParser& tag_parser() const;
/*!
* \brief Imageboard post URL deduced from tags (might be empty).
*/
QString postURL() const;
/*!
* \brief Imageboard post API url deduced from tags (might be empty).
*/
QString postTagsApiURL() const;
/*!
* \brief Are tag file(s) present.
*/
bool hasTagFile() const;
/*!
* \brief List of tags added by user.
* \param exclude_tags_from_file If \c true, only tags not present
* in tag file will be returned.
*/
QStringList getAddedTags(bool exclude_tags_from_file = false) const;
/*!
* \brief Set tag input view mode.
*/
void setViewMode(ViewMode mode);
/*!
* \brief Returns removed tags color.
*/
QColor getRemovedTagColor() const;
/*!
* \brief Sets removed tags color.
*/
void setRemovedTagColor(QColor color);
/*!
* \brief Returns replaced tags color.
*/
QColor getReplacedTagColor() const;
/*!
* \brief Sets replaced tags color.
*/
void setReplacedTagColor(QColor color);
/*!
* \brief Returns unknown tags color.
*/
QColor getUnknownTagColor() const;
/*!
* \brief Sets unknown tags color.
*/
void setUnknownTagColor(QColor color);
/*!
* \brief Returns naming mode background color.
*/
QColor getNamingModeBackgroundColor() const;
/*!
* \brief Sets naming mode background color.
*/
void setNamingModeBackgroundColor(QColor color);
/*!
* \brief Returns pointer to current tag autocomplete model.
*/
QAbstractItemModel* completionModel();
signals:
/*!
* \brief Emitted when tag file parse error occured
* \param regex_source Regular expression source string
* \param error Human-readable parsing error description
* \param column Error position in the string
*/
void parseError(QString regex_source, QString error, int column);
protected:
void keyPressEvent(QKeyEvent* event) override;
void focusInEvent(QFocusEvent* event) override;
private:
bool next_completer();
void updateText(const QString &t);
/// Classify all tags in current list and style accordingly.
void classifyText(const QStringList & tag_list);
/// Add negated versions of all tags in current list and append to model.
void updateModelRemovedTags(const QStringList& tag_list);
void update_qss();
static constexpr int m_minimum_height = 30;
static constexpr int m_minimum_height_minmode = 25;
/// Tag file parser and tag fixer
TagParser m_tag_parser;
/// Editing state for current text.
TagEditState m_edit_state;
int m_index = 0;
QStringList m_text_list;
QString m_initial_text;
int m_path_length_limit = 0;
ViewMode m_view_mode = ViewMode::Normal;
EditMode m_edit_mode = EditMode::Tagging;
/*!
* \brief A model used to display completions with comments.
*
* In order for auto-completion to ignore the comment text, original
* tags are inserted into \c Qt::UserRole, while display tags are inserted
* into \c Qt::DisplayRole.
*/
QStandardItemModel m_tags_model;
/// Number of main tags in model.
int m_tags_model_num_main_tags = 0;
std::unique_ptr<MultiSelectCompleter> m_completer;
using tag_iterator = decltype(std::begin(m_text_list));
QColor m_removed_tag_color;
QColor m_replaced_tag_color;
QColor m_unknown_tag_color;
QColor m_naming_mode_color;
};
#endif // TAGINPUT_H