1 #include "HighlightModel.hpp"
2 
3 #include "Application.hpp"
4 #include "singletons/Settings.hpp"
5 #include "singletons/WindowManager.hpp"
6 #include "util/StandardItemHelper.hpp"
7 
8 namespace chatterino {
9 
10 // commandmodel
HighlightModel(QObject * parent)11 HighlightModel::HighlightModel(QObject *parent)
12     : SignalVectorModel<HighlightPhrase>(Column::COUNT, parent)
13 {
14 }
15 
16 // turn a vector item into a model row
getItemFromRow(std::vector<QStandardItem * > & row,const HighlightPhrase & original)17 HighlightPhrase HighlightModel::getItemFromRow(
18     std::vector<QStandardItem *> &row, const HighlightPhrase &original)
19 {
20     // In order for old messages to update their highlight color, we need to
21     // update the highlight color here.
22     auto highlightColor = original.getColor();
23     *highlightColor =
24         row[Column::Color]->data(Qt::DecorationRole).value<QColor>();
25 
26     return HighlightPhrase{
27         row[Column::Pattern]->data(Qt::DisplayRole).toString(),
28         row[Column::ShowInMentions]->data(Qt::CheckStateRole).toBool(),
29         row[Column::FlashTaskbar]->data(Qt::CheckStateRole).toBool(),
30         row[Column::PlaySound]->data(Qt::CheckStateRole).toBool(),
31         row[Column::UseRegex]->data(Qt::CheckStateRole).toBool(),
32         row[Column::CaseSensitive]->data(Qt::CheckStateRole).toBool(),
33         row[Column::SoundPath]->data(Qt::UserRole).toString(),
34         highlightColor};
35 }
36 
37 // turns a row in the model into a vector item
getRowFromItem(const HighlightPhrase & item,std::vector<QStandardItem * > & row)38 void HighlightModel::getRowFromItem(const HighlightPhrase &item,
39                                     std::vector<QStandardItem *> &row)
40 {
41     setStringItem(row[Column::Pattern], item.getPattern());
42     setBoolItem(row[Column::ShowInMentions], item.showInMentions());
43     setBoolItem(row[Column::FlashTaskbar], item.hasAlert());
44     setBoolItem(row[Column::PlaySound], item.hasSound());
45     setBoolItem(row[Column::UseRegex], item.isRegex());
46     setBoolItem(row[Column::CaseSensitive], item.isCaseSensitive());
47     setFilePathItem(row[Column::SoundPath], item.getSoundUrl());
48     setColorItem(row[Column::Color], *item.getColor());
49 }
50 
afterInit()51 void HighlightModel::afterInit()
52 {
53     // Highlight settings for own username
54     std::vector<QStandardItem *> usernameRow = this->createRow();
55     setBoolItem(usernameRow[Column::Pattern],
56                 getSettings()->enableSelfHighlight.getValue(), true, false);
57     usernameRow[Column::Pattern]->setData("Your username (automatic)",
58                                           Qt::DisplayRole);
59     setBoolItem(usernameRow[Column::ShowInMentions],
60                 getSettings()->showSelfHighlightInMentions.getValue(), true,
61                 false);
62     setBoolItem(usernameRow[Column::FlashTaskbar],
63                 getSettings()->enableSelfHighlightTaskbar.getValue(), true,
64                 false);
65     setBoolItem(usernameRow[Column::PlaySound],
66                 getSettings()->enableSelfHighlightSound.getValue(), true,
67                 false);
68     usernameRow[Column::UseRegex]->setFlags({});
69     usernameRow[Column::CaseSensitive]->setFlags({});
70 
71     QUrl selfSound = QUrl(getSettings()->selfHighlightSoundUrl.getValue());
72     setFilePathItem(usernameRow[Column::SoundPath], selfSound, false);
73 
74     auto selfColor = ColorProvider::instance().color(ColorType::SelfHighlight);
75     setColorItem(usernameRow[Column::Color], *selfColor, false);
76 
77     this->insertCustomRow(usernameRow, {});
78 
79     // Highlight settings for whispers
80     std::vector<QStandardItem *> whisperRow = this->createRow();
81     setBoolItem(whisperRow[Column::Pattern],
82                 getSettings()->enableWhisperHighlight.getValue(), true, false);
83     whisperRow[Column::Pattern]->setData("Whispers", Qt::DisplayRole);
84     whisperRow[Column::ShowInMentions]->setFlags({});  // We have /whispers
85     setBoolItem(whisperRow[Column::FlashTaskbar],
86                 getSettings()->enableWhisperHighlightTaskbar.getValue(), true,
87                 false);
88     setBoolItem(whisperRow[Column::PlaySound],
89                 getSettings()->enableWhisperHighlightSound.getValue(), true,
90                 false);
91     whisperRow[Column::UseRegex]->setFlags({});
92     whisperRow[Column::CaseSensitive]->setFlags({});
93 
94     QUrl whisperSound =
95         QUrl(getSettings()->whisperHighlightSoundUrl.getValue());
96     setFilePathItem(whisperRow[Column::SoundPath], whisperSound, false);
97 
98     //    auto whisperColor = ColorProvider::instance().color(ColorType::Whisper);
99     //    setColorItem(whisperRow[Column::Color], *whisperColor, false);
100     whisperRow[Column::Color]->setFlags(Qt::ItemFlag::NoItemFlags);
101 
102     this->insertCustomRow(whisperRow, WHISPER_ROW);
103 
104     // Highlight settings for subscription messages
105     std::vector<QStandardItem *> subRow = this->createRow();
106     setBoolItem(subRow[Column::Pattern],
107                 getSettings()->enableSubHighlight.getValue(), true, false);
108     subRow[Column::Pattern]->setData("Subscriptions", Qt::DisplayRole);
109     subRow[Column::ShowInMentions]->setFlags({});
110     setBoolItem(subRow[Column::FlashTaskbar],
111                 getSettings()->enableSubHighlightTaskbar.getValue(), true,
112                 false);
113     setBoolItem(subRow[Column::PlaySound],
114                 getSettings()->enableSubHighlightSound.getValue(), true, false);
115     subRow[Column::UseRegex]->setFlags({});
116     subRow[Column::CaseSensitive]->setFlags({});
117 
118     QUrl subSound = QUrl(getSettings()->subHighlightSoundUrl.getValue());
119     setFilePathItem(subRow[Column::SoundPath], subSound, false);
120 
121     auto subColor = ColorProvider::instance().color(ColorType::Subscription);
122     setColorItem(subRow[Column::Color], *subColor, false);
123 
124     this->insertCustomRow(subRow, 2);
125 
126     // Highlight settings for redeemed highlight messages
127     std::vector<QStandardItem *> redeemedRow = this->createRow();
128     setBoolItem(redeemedRow[Column::Pattern],
129                 getSettings()->enableRedeemedHighlight.getValue(), true, false);
130     redeemedRow[Column::Pattern]->setData(
131         "Highlights redeemed with Channel Points", Qt::DisplayRole);
132     redeemedRow[Column::ShowInMentions]->setFlags({});
133     //    setBoolItem(redeemedRow[Column::FlashTaskbar],
134     //                getSettings()->enableRedeemedHighlightTaskbar.getValue(), true,
135     //                false);
136     //    setBoolItem(redeemedRow[Column::PlaySound],
137     //                getSettings()->enableRedeemedHighlightSound.getValue(), true,
138     //                false);
139     redeemedRow[Column::FlashTaskbar]->setFlags({});
140     redeemedRow[Column::PlaySound]->setFlags({});
141     redeemedRow[Column::UseRegex]->setFlags({});
142     redeemedRow[Column::CaseSensitive]->setFlags({});
143 
144     QUrl RedeemedSound =
145         QUrl(getSettings()->redeemedHighlightSoundUrl.getValue());
146     setFilePathItem(redeemedRow[Column::SoundPath], RedeemedSound, false);
147 
148     auto RedeemedColor =
149         ColorProvider::instance().color(ColorType::RedeemedHighlight);
150     setColorItem(redeemedRow[Column::Color], *RedeemedColor, false);
151 
152     this->insertCustomRow(redeemedRow, 3);
153 }
154 
customRowSetData(const std::vector<QStandardItem * > & row,int column,const QVariant & value,int role,int rowIndex)155 void HighlightModel::customRowSetData(const std::vector<QStandardItem *> &row,
156                                       int column, const QVariant &value,
157                                       int role, int rowIndex)
158 {
159     switch (column)
160     {
161         case Column::Pattern: {
162             if (role == Qt::CheckStateRole)
163             {
164                 if (rowIndex == 0)
165                 {
166                     getSettings()->enableSelfHighlight.setValue(value.toBool());
167                 }
168                 else if (rowIndex == WHISPER_ROW)
169                 {
170                     getSettings()->enableWhisperHighlight.setValue(
171                         value.toBool());
172                 }
173                 else if (rowIndex == 2)
174                 {
175                     getSettings()->enableSubHighlight.setValue(value.toBool());
176                 }
177                 else if (rowIndex == 3)
178                 {
179                     getSettings()->enableRedeemedHighlight.setValue(
180                         value.toBool());
181                 }
182             }
183         }
184         break;
185         case Column::ShowInMentions: {
186             if (role == Qt::CheckStateRole)
187             {
188                 if (rowIndex == 0)
189                 {
190                     getSettings()->showSelfHighlightInMentions.setValue(
191                         value.toBool());
192                 }
193             }
194         }
195         break;
196         case Column::FlashTaskbar: {
197             if (role == Qt::CheckStateRole)
198             {
199                 if (rowIndex == 0)
200                 {
201                     getSettings()->enableSelfHighlightTaskbar.setValue(
202                         value.toBool());
203                 }
204                 else if (rowIndex == WHISPER_ROW)
205                 {
206                     getSettings()->enableWhisperHighlightTaskbar.setValue(
207                         value.toBool());
208                 }
209                 else if (rowIndex == 2)
210                 {
211                     getSettings()->enableSubHighlightTaskbar.setValue(
212                         value.toBool());
213                 }
214                 else if (rowIndex == 3)
215                 {
216                     // getSettings()->enableRedeemedHighlightTaskbar.setValue(
217                     //     value.toBool());
218                 }
219             }
220         }
221         break;
222         case Column::PlaySound: {
223             if (role == Qt::CheckStateRole)
224             {
225                 if (rowIndex == 0)
226                 {
227                     getSettings()->enableSelfHighlightSound.setValue(
228                         value.toBool());
229                 }
230                 else if (rowIndex == WHISPER_ROW)
231                 {
232                     getSettings()->enableWhisperHighlightSound.setValue(
233                         value.toBool());
234                 }
235                 else if (rowIndex == 2)
236                 {
237                     getSettings()->enableSubHighlightSound.setValue(
238                         value.toBool());
239                 }
240                 else if (rowIndex == 3)
241                 {
242                     // getSettings()->enableRedeemedHighlightSound.setValue(
243                     //     value.toBool());
244                 }
245             }
246         }
247         break;
248         case Column::UseRegex: {
249             // Regex --> empty
250         }
251         break;
252         case Column::CaseSensitive: {
253             // Case-sensitivity --> empty
254         }
255         break;
256         case Column::SoundPath: {
257             // Custom sound file
258             if (role == Qt::UserRole)
259             {
260                 if (rowIndex == 0)
261                 {
262                     getSettings()->selfHighlightSoundUrl.setValue(
263                         value.toString());
264                 }
265                 else if (rowIndex == WHISPER_ROW)
266                 {
267                     getSettings()->whisperHighlightSoundUrl.setValue(
268                         value.toString());
269                 }
270                 else if (rowIndex == 2)
271                 {
272                     getSettings()->subHighlightSoundUrl.setValue(
273                         value.toString());
274                 }
275                 else if (rowIndex == 3)
276                 {
277                     getSettings()->redeemedHighlightSoundUrl.setValue(
278                         value.toString());
279                 }
280             }
281         }
282         break;
283         case Column::Color: {
284             // Custom color
285             if (role == Qt::DecorationRole)
286             {
287                 auto colorName = value.value<QColor>().name(QColor::HexArgb);
288                 if (rowIndex == 0)
289                 {
290                     getSettings()->selfHighlightColor.setValue(colorName);
291                 }
292                 //                else if (rowIndex == WHISPER_ROW)
293                 //                {
294                 //                    getSettings()->whisperHighlightColor.setValue(colorName);
295                 //                }
296                 else if (rowIndex == 2)
297                 {
298                     getSettings()->subHighlightColor.setValue(colorName);
299                 }
300                 else if (rowIndex == 3)
301                 {
302                     getSettings()->redeemedHighlightColor.setValue(colorName);
303                     const_cast<ColorProvider &>(ColorProvider::instance())
304                         .updateColor(ColorType::RedeemedHighlight,
305                                      QColor(colorName));
306                 }
307             }
308         }
309         break;
310     }
311 
312     getApp()->windows->forceLayoutChannelViews();
313 }
314 
315 }  // namespace chatterino
316