-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
193 lines (155 loc) · 5.14 KB
/
Window.cpp
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
#include "Window.h"
#include <QCoreApplication>
#include <QSystemTrayIcon>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QStandardItemModel>
#include <QListView>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QLineEdit>
#include <QAction>
#include <QLabel>
#include <QVBoxLayout>
Window::Window()
{
setWindowFlag(Qt::Dialog);
setLayout(new QVBoxLayout);
// Tray icon
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(QPixmap(":/icon.png"), this);
trayIcon->show();
connect(trayIcon, &QSystemTrayIcon::activated, this, [=]() { setVisible(!isVisible()); });
// Quit action (ctrl+q I guess)
QAction *quitAction = new QAction("Quit", this);
quitAction->setShortcut(QKeySequence::Quit);
addAction(quitAction);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
// Model with the actual tasks
m_listModel = new QStandardItemModel(this);
// For sorting checked tasks below unchecked, and searching/filtering
m_filterModel = new QSortFilterProxyModel(this);
m_filterModel->setRecursiveFilteringEnabled(true);
m_filterModel->setSourceModel(m_listModel);
m_filterModel->setSortRole(Qt::CheckStateRole);
// List of tasks
m_listView = new QListView(this);
m_listView->setModel(m_filterModel);
m_listView->setDragDropMode(QAbstractItemView::InternalMove);
// Add new item edit
m_addEdit = new QLineEdit(this);
m_addEdit->setPlaceholderText("Enter toldyouso item...");
m_addEdit->setClearButtonEnabled(true);
connect(m_addEdit, &QLineEdit::returnPressed, this, &Window::onAddAccepted);
// Search edit
QLineEdit *filterEdit = new QLineEdit(this);
filterEdit->setPlaceholderText("Search...");
filterEdit->setClearButtonEnabled(true);
connect(filterEdit, &QLineEdit::textChanged, m_filterModel, &QSortFilterProxyModel::setFilterFixedString);
connect(filterEdit, SIGNAL(returnPressed()), m_listView, SLOT(setFocus())); // the qOverride crap is so fucking ugly
m_score = new QLabel("0");
// Filter/search on top, the list in the middle, entering new item at the bottom
layout()->addWidget(m_score);
layout()->addWidget(filterEdit);
layout()->addWidget(m_listView);
layout()->addWidget(m_addEdit);
m_addEdit->setFocus();
load();
connect(m_filterModel, &QSortFilterProxyModel::rowsMoved, this, &Window::save);
connect(m_listModel, &QStandardItemModel::itemChanged, this, &Window::onItemChanged);
resize(500, 750);
}
void Window::onAddAccepted()
{
if (m_addEdit->text().isEmpty()) {
return;
}
addItem(m_addEdit->text(), false);
m_addEdit->clear();
updateScore();
save();
}
void Window::load()
{
QFile inputFile(QDir::home().filePath("toldyouso.txt"));
if (!inputFile.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open" << inputFile.fileName() << inputFile.errorString();
return;
}
QStringList checked;
QStringList unchecked;
while (!inputFile.atEnd()) {
QString inputLine = QString::fromUtf8(inputFile.readLine()).trimmed();
if (inputLine.isEmpty()) {
continue;
}
const QChar state = inputLine[0];
inputLine.remove(0, 1);
if (state == 'x') {
checked.prepend(inputLine.trimmed());
} else {
unchecked.prepend(inputLine.trimmed());
}
}
for (const QString &text : checked) {
addItem(text, true);
}
for (const QString &text : unchecked) {
addItem(text, false);
}
updateScore();
}
void Window::save() const
{
QFile outputFile(QDir::home().filePath("toldyouso.txt"));
if (!outputFile.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to open" << outputFile.fileName() << outputFile.errorString();
return;
}
for (int row=0; row<m_listModel->rowCount(); row++) {
QStandardItem *item = m_listModel->item(row);
if (item->text().isEmpty()) {
continue;
}
if (item->checkState() == Qt::Checked) {
outputFile.write(" x ");
} else {
outputFile.write(" - ");
}
outputFile.write(item->text().toUtf8() + '\n');
}
}
void Window::onItemChanged(QStandardItem *item)
{
if (item->text().isEmpty()) {
m_listModel->removeRow(item->row(), item->parent() ? item->parent()->index() : QModelIndex());
}
m_filterModel->sort(0);
updateScore();
save();
}
void Window::addItem(const QString &text, const bool checked)
{
if (text.isEmpty()) {
return;
}
QStandardItem *item = new QStandardItem(text);
item->setCheckable(true);
item->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
item->setDropEnabled(false);
m_listModel->insertRow(0, item);
updateScore();
}
void Window::updateScore()
{
double score = 0;
for (int row=0; row<m_listModel->rowCount(); row++) {
if (m_listModel->item(row)->checkState() == Qt::Checked) {
if (!score) {
score = 1;
}
score *= 1.5;
}
}
m_score->setText("<h1>Score: <b>" + QString::number(score, 'f', 1) + "</b></h1>");
}