File indexing completed on 2024-10-06 05:12:46
0001 /** 0002 * SPDX-FileCopyrightText: (C) 2005 Sébastien Laoût <slaout@linux62.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef TAG_H 0008 #define TAG_H 0009 0010 #include <QtCore/QList> 0011 #include <QRegularExpression> 0012 #include <KToggleAction> 0013 0014 class QColor; 0015 class QFont; 0016 class QPainter; 0017 class QString; 0018 0019 0020 class QKeySequence; 0021 0022 class Tag; 0023 0024 /** 0025 * @author Sébastien Laoût 0026 */ 0027 class State 0028 { 0029 public: 0030 /// LIST OF STATES: 0031 typedef QList<State *> List; 0032 0033 public: 0034 /// CONSTRUCTOR AND DESTRUCTOR: 0035 explicit State(const QString &id = QString(), Tag *tag = nullptr); 0036 ~State(); 0037 /// SET PROPERTIES: 0038 void setId(const QString &id) 0039 { 0040 m_id = id; 0041 } 0042 void setName(const QString &name) 0043 { 0044 m_name = name; 0045 } 0046 void setEmblem(const QString &emblem) 0047 { 0048 m_emblem = emblem; 0049 } 0050 void setBold(bool bold) 0051 { 0052 m_bold = bold; 0053 } 0054 void setItalic(bool italic) 0055 { 0056 m_italic = italic; 0057 } 0058 void setUnderline(bool underline) 0059 { 0060 m_underline = underline; 0061 } 0062 void setStrikeOut(bool strikeOut) 0063 { 0064 m_strikeOut = strikeOut; 0065 } 0066 void setTextColor(const QColor &color) 0067 { 0068 m_textColor = color; 0069 } 0070 void setFontName(const QString &font) 0071 { 0072 m_fontName = font; 0073 } 0074 void setFontSize(int size) 0075 { 0076 m_fontSize = size; 0077 } 0078 void setBackgroundColor(const QColor &color) 0079 { 0080 m_backgroundColor = color; 0081 } 0082 void setTextEquivalent(const QString &text) 0083 { 0084 m_textEquivalent = text; 0085 } 0086 void setOnAllTextLines(bool yes) 0087 { 0088 m_onAllTextLines = yes; 0089 } 0090 void setAllowCrossReferences(bool yes) 0091 { 0092 m_allowCrossReferences = yes; 0093 } 0094 void setParentTag(Tag *tag) 0095 { 0096 m_parentTag = tag; 0097 } 0098 /// GET PROPERTIES: 0099 QString id() const 0100 { 0101 return m_id; 0102 } 0103 QString name() const 0104 { 0105 return m_name; 0106 } 0107 QString emblem() const 0108 { 0109 return m_emblem; 0110 } 0111 bool bold() const 0112 { 0113 return m_bold; 0114 } 0115 bool italic() const 0116 { 0117 return m_italic; 0118 } 0119 bool underline() const 0120 { 0121 return m_underline; 0122 } 0123 bool strikeOut() const 0124 { 0125 return m_strikeOut; 0126 } 0127 QColor textColor() const 0128 { 0129 return m_textColor; 0130 } 0131 QString fontName() const 0132 { 0133 return m_fontName; 0134 } 0135 int fontSize() const 0136 { 0137 return m_fontSize; 0138 } 0139 QColor backgroundColor() const 0140 { 0141 return m_backgroundColor; 0142 } 0143 QString textEquivalent() const 0144 { 0145 return m_textEquivalent; 0146 } 0147 bool onAllTextLines() const 0148 { 0149 return m_onAllTextLines; 0150 } 0151 bool allowCrossReferences() const 0152 { 0153 return m_allowCrossReferences; 0154 } 0155 Tag *parentTag() const 0156 { 0157 return m_parentTag; 0158 } 0159 /// HELPING FUNCTIONS: 0160 State *nextState(bool cycle = true); 0161 QString fullName(); 0162 QFont font(QFont base); 0163 QString toCSS(const QString &gradientFolderPath, const QString &gradientFolderName, const QFont &baseFont); 0164 static void merge(const List &states, State *result, int *emblemsCount, bool *haveInvisibleTags, const QColor &backgroundColor); 0165 void copyTo(State *other); 0166 0167 private: 0168 /// PROPERTIES: 0169 QString m_id; 0170 QString m_name; 0171 QString m_emblem; 0172 bool m_bold; 0173 bool m_italic; 0174 bool m_underline; 0175 bool m_strikeOut; 0176 QColor m_textColor; 0177 QString m_fontName; 0178 int m_fontSize; 0179 QColor m_backgroundColor; 0180 QString m_textEquivalent; 0181 bool m_onAllTextLines; 0182 bool m_allowCrossReferences; 0183 Tag *m_parentTag; 0184 }; 0185 0186 /** A Tag is a category of Notes. 0187 * A Note can have 0, 1 or more Tags. 0188 * A Tag can have a unique State or several States. 0189 * @author Sébastien Laoût 0190 */ 0191 class Tag 0192 { 0193 public: 0194 /// LIST OF ALL TAGS IN THE APPLICATION: 0195 typedef QList<Tag *> List; 0196 static Tag::List all; 0197 static State *stateById(const QString &id); 0198 static State *stateByTextEquiv(const QString &text); 0199 static Tag *tagForKAction(QAction *action); 0200 static Tag *tagSimilarTo(Tag *tagToTest); 0201 static QMap<QString, QString> loadTags(const QString &path = QString() /*, bool merge = false*/); /// << Load the tags contained in the XML file @p path or those in the application settings if @p path isEmpty(). If @p merge is true and 0202 /// a tag with the id of a tag that should be loaded already exist, the tag will get a new id. Otherwise, the tag will be dismissed. 0203 static void saveTags(); 0204 static void saveTagsTo(QList<Tag *> &list, const QString &fullPath); 0205 static void createDefaultTagsSet(const QString &file); 0206 static long getNextStateUid(); 0207 static void updateCaches(); 0208 static const QRegularExpression& regexpDetectTagsInPlainText(); 0209 0210 private: 0211 static long nextStateUid; 0212 static QHash<QString, State*> dictStatesByEquiv; 0213 static QRegularExpression regexpDetectTags; 0214 0215 public: 0216 /// CONSTRUCTOR AND DESTRUCTOR: 0217 Tag(/*State *firstState, const QString &name, bool inheritedBySiblings*/); 0218 ~Tag(); 0219 /// SET PROPERTIES: 0220 void setName(const QString &name); 0221 void setShortcut(const QKeySequence &shortcut) 0222 { 0223 m_action->setShortcut(shortcut); 0224 } 0225 void setInheritedBySiblings(bool inherited) 0226 { 0227 m_inheritedBySiblings = inherited; 0228 } 0229 void appendState(State *state) 0230 { 0231 m_states.append(state); 0232 state->setParentTag(this); 0233 } 0234 void removeState(State *state) 0235 { 0236 m_states.removeOne(state); 0237 state->setParentTag(nullptr); 0238 } 0239 /// GET PROPERTIES: 0240 QString name() const 0241 { 0242 return m_name; 0243 } 0244 QKeySequence shortcut() const 0245 { 0246 return m_action->shortcut(); 0247 } 0248 bool inheritedBySiblings() const 0249 { 0250 return m_inheritedBySiblings; 0251 } 0252 State::List &states() const 0253 { 0254 return (State::List &)m_states; 0255 } 0256 int countStates() const 0257 { 0258 return m_states.count(); 0259 } 0260 void copyTo(Tag *other); 0261 0262 private: 0263 /// PROPERTIES: 0264 QString m_name; 0265 QAction *m_action; 0266 bool m_inheritedBySiblings; 0267 State::List m_states; 0268 }; 0269 0270 #include <qicon.h> 0271 #include <qstring.h> 0272 0273 /** An action that represents a State or a Tag 0274 * @author Kelvie Wong 0275 * Based off of StateMenuItem by Sébastien Laoût 0276 */ 0277 class StateAction : public KToggleAction 0278 { 0279 Q_OBJECT 0280 Q_DISABLE_COPY(StateAction); 0281 0282 public: 0283 StateAction(State *state, const QKeySequence &shortcut, QWidget *parent, bool withTagName = false); 0284 0285 ~StateAction() override; 0286 0287 private: 0288 State *m_state; 0289 QString m_name; 0290 QString m_shortcut; 0291 }; 0292 0293 #endif // TAG_H