summaryrefslogtreecommitdiff
path: root/src/colortable.cpp
blob: 121df708d6d5ab63ed3bf7c6cbbe99c06fb29f4c (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "colortable.h"
#include "modconflicticondelegate.h"
#include "modflagicondelegate.h"
#include "settings.h"

class ColorItem;
ColorItem* colorItemForRow(QTableWidget* table, int row);

void paintBackground(QTableWidget* table, QPainter* p,
                     const QStyleOptionViewItem& option, const QModelIndex& index);

// delegate for the sample text column; paints the background color
//
class ColoredBackgroundDelegate : public QStyledItemDelegate
{
public:
  ColoredBackgroundDelegate(QTableWidget* table) : m_table(table) {}

  void paint(QPainter* p, const QStyleOptionViewItem& option,
             const QModelIndex& index) const override
  {
    paintBackground(m_table, p, option, index);

    QStyleOptionViewItem itemOption(option);
    initStyleOption(&itemOption, index);

    // paint the default stuff like text, but override the state to avoid
    // destroying the background for selected items, etc.
    itemOption.state = QStyle::State_Enabled;

    QStyledItemDelegate::paint(p, itemOption, index);
  }

private:
  QTableWidget* m_table;
};

// delegate for the icons column; paints the background and icons
//
class FakeModFlagIconDelegate : public ModFlagIconDelegate
{
public:
  explicit FakeModFlagIconDelegate(QTableWidget* table) : m_table(table) {}

  void paint(QPainter* painter, const QStyleOptionViewItem& option,
             const QModelIndex& index) const override
  {
    paintBackground(m_table, painter, option, index);
    ModFlagIconDelegate::paintIcons(painter, option, index, getIcons(index));
  }

protected:
  QList<QString> getIcons(const QModelIndex& index) const override
  {
    const auto flags = {ModInfo::FLAG_BACKUP, ModInfo::FLAG_NOTENDORSED,
                        ModInfo::FLAG_NOTES, ModInfo::FLAG_ALTERNATE_GAME};

    return getIconsForFlags(flags, false);
  }

  size_t getNumIcons(const QModelIndex& index) const override
  {
    return getIcons(index).size();
  }

private:
  QTableWidget* m_table;
};

// delegate for the icons column; paints the background and icons
//
class FakeModConflictIconDelegate : public ModConflictIconDelegate
{
public:
  explicit FakeModConflictIconDelegate(QTableWidget* table) : m_table(table) {}

  void paint(QPainter* painter, const QStyleOptionViewItem& option,
             const QModelIndex& index) const override
  {
    paintBackground(m_table, painter, option, index);
    ModFlagIconDelegate::paintIcons(painter, option, index, getIcons(index));
  }

protected:
  QList<QString> getIcons(const QModelIndex& index) const override
  {
    const auto flags = {ModInfo::FLAG_CONFLICT_MIXED,
                        ModInfo::FLAG_ARCHIVE_LOOSE_CONFLICT_OVERWRITE,
                        ModInfo::FLAG_ARCHIVE_LOOSE_CONFLICT_OVERWRITTEN,
                        ModInfo::FLAG_ARCHIVE_CONFLICT_MIXED};

    return getIconsForFlags(flags, false);
  }

  size_t getNumIcons(const QModelIndex& index) const override
  {
    return getIcons(index).size();
  }

private:
  QTableWidget* m_table;
};

// item used in the first column of the table
//
class ColorItem : public QTableWidgetItem
{
public:
  ColorItem(QString caption, QColor defaultColor, std::function<QColor()> get,
            std::function<void(const QColor&)> commit)
      : m_caption(std::move(caption)), m_default(defaultColor), m_get(get),
        m_commit(commit)
  {
    setText(m_caption);
    set(get());
  }

  // color caption
  //
  const QString& caption() const { return m_caption; }

  // the current color
  //
  QColor get() const { return m_temp; }

  // sets the current color, commit() must be called to save it
  //
  bool set(const QColor& c)
  {
    if (m_temp != c) {
      m_temp = c;
      return true;
    }

    return false;
  }

  // resets the current color, commit() must be called to save it
  //
  bool reset() { return set(m_default); }

  // saves the current color
  //
  void commit() { m_commit(m_temp); }

private:
  const QString m_caption;
  const QColor m_default;
  std::function<QColor()> m_get;
  std::function<void(const QColor&)> m_commit;
  QColor m_temp;
};

ColorItem* colorItemForRow(QTableWidget* table, int row)
{
  return dynamic_cast<ColorItem*>(table->item(row, 0));
}

template <class F>
void forEachColorItem(QTableWidget* table, F&& f)
{
  const auto rowCount = table->rowCount();

  for (int i = 0; i < rowCount; ++i) {
    if (auto* ci = colorItemForRow(table, i)) {
      f(ci);
    }
  }
}

void paintBackground(QTableWidget* table, QPainter* p,
                     const QStyleOptionViewItem& option, const QModelIndex& index)
{
  if (auto* ci = colorItemForRow(table, index.row())) {
    p->save();
    p->fillRect(option.rect, ci->get());
    p->restore();
  }
}

ColorTable::ColorTable(QWidget* parent) : QTableWidget(parent), m_settings(nullptr)
{
  setColumnCount(4);
  setHorizontalHeaderLabels({"", "", "", ""});

  setItemDelegateForColumn(1, new ColoredBackgroundDelegate(this));
  setItemDelegateForColumn(2, new FakeModConflictIconDelegate(this));
  setItemDelegateForColumn(3, new FakeModFlagIconDelegate(this));

  connect(this, &QTableWidget::cellActivated, [&] {
    onColorActivated();
  });
}

void ColorTable::load(Settings& s)
{
  m_settings = &s;

  addColor(
      QObject::tr("Is overwritten (loose files)"), QColor(0, 255, 0, 64),
      [this] {
        return m_settings->colors().modlistOverwrittenLoose();
      },
      [this](auto&& v) {
        m_settings->colors().setModlistOverwrittenLoose(v);
      });

  addColor(
      QObject::tr("Is overwriting (loose files)"), QColor(255, 0, 0, 64),
      [this] {
        return m_settings->colors().modlistOverwritingLoose();
      },
      [this](auto&& v) {
        m_settings->colors().setModlistOverwritingLoose(v);
      });

  addColor(
      QObject::tr("Is overwritten (archives)"), QColor(0, 255, 255, 64),
      [this] {
        return m_settings->colors().modlistOverwrittenArchive();
      },
      [this](auto&& v) {
        m_settings->colors().setModlistOverwrittenArchive(v);
      });

  addColor(
      QObject::tr("Is overwriting (archives)"), QColor(255, 0, 255, 64),
      [this] {
        return m_settings->colors().modlistOverwritingArchive();
      },
      [this](auto&& v) {
        m_settings->colors().setModlistOverwritingArchive(v);
      });

  addColor(
      QObject::tr("Mod contains selected file"), QColor(0, 0, 255, 64),
      [this] {
        return m_settings->colors().modlistContainsFile();
      },
      [this](auto&& v) {
        m_settings->colors().setModlistContainsFile(v);
      });

  addColor(
      QObject::tr("Plugin is contained in selected mod"), QColor(0, 0, 255, 64),
      [this] {
        return m_settings->colors().pluginListContained();
      },
      [this](auto&& v) {
        m_settings->colors().setPluginListContained(v);
      });

  addColor(
      QObject::tr("Plugin is master of selected plugin"), QColor(255, 255, 0, 64),
      [this] {
        return m_settings->colors().pluginListMaster();
      },
      [this](auto&& v) {
        m_settings->colors().setPluginListMaster(v);
      });
}

void ColorTable::resetColors()
{
  bool changed = false;

  forEachColorItem(this, [&](auto* item) {
    if (item->reset()) {
      changed = true;
    }
  });

  if (changed) {
    update();
  }
}

void ColorTable::commitColors()
{
  forEachColorItem(this, [](auto* item) {
    item->commit();
  });
}

void ColorTable::addColor(const QString& text, const QColor& defaultColor,
                          std::function<QColor()> get,
                          std::function<void(const QColor&)> commit)
{
  const auto r = rowCount();
  setRowCount(r + 1);

  setItem(r, 0, new ColorItem(text, defaultColor, get, commit));
  setItem(r, 1, new QTableWidgetItem("Text"));
  setItem(r, 2, new QTableWidgetItem);

  resizeColumnsToContents();
}

void ColorTable::onColorActivated()
{
  const auto rows = selectionModel()->selectedRows();
  if (rows.isEmpty()) {
    return;
  }

  const auto row = rows[0].row();
  auto* ci       = colorItemForRow(this, row);
  if (!ci) {
    return;
  }

  const QColor result = QColorDialog::getColor(
      ci->get(), topLevelWidget(), ci->caption(), QColorDialog::ShowAlphaChannel);

  if (result.isValid()) {
    ci->set(result);
    update(model()->index(row, 1));
  }
}