-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlistwidgethandler.cpp
132 lines (100 loc) · 2.41 KB
/
listwidgethandler.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
#include "listwidgethandler.h"
#include "traceentry.h"
#include <QListWidget>
#include <QSharedPointer>
namespace Templar {
namespace Detail {
class Command
{
public:
virtual ~Command() {}
virtual void apply(ListWidgetHandler *handler) = 0;
};
class AddCommand : public Command
{
public:
AddCommand(QListWidgetItem *item) :
item(item) {}
void apply(ListWidgetHandler *handler) {
handler->addItem(item);
}
private:
QListWidgetItem *item;
};
class TakeCommand : public Command
{
public:
void apply(ListWidgetHandler *handler) {
QSharedPointer<QListWidgetItem> item(handler->takeItem());
(void)item;
}
};
} // namespace Detail
void ListWidgetHandler::handleEvent(const TraceEntry &entry)
{
using namespace Detail;
if (entry.isBegin) {
QListWidgetItem *item = makeItem(entry);
addItem(item);
Command *command (new TakeCommand());
undoList.push_back(command);
} else {
QListWidgetItem *item = takeItem();
Command *command (new AddCommand(item));
undoList.push_back(command);
}
}
void ListWidgetHandler::undoEvent()
{
if (undoList.empty())
return;
using namespace Detail;
QSharedPointer<Command> undo (undoList.back());
undoList.pop_back();
undo->apply(this);
}
void ListWidgetHandler::reset()
{
if (!(listWidget->count() == 0))
listWidget->clear();
if (!undoList.empty())
undoList.clear();
}
void ListWidgetHandler::addItem(QListWidgetItem *item)
{
listWidget->addItem(item);
listWidget->scrollToItem(item);
}
QListWidgetItem *ListWidgetHandler::takeItem()
{
QListWidgetItem *item = listWidget->takeItem(listWidget->count() - 1);
return item;
}
QListWidgetItem* ListWidgetHandler::makeItem(const TraceEntry &entry) const
{
QListWidgetItem *item = new QListWidgetItem(entry.context, listWidget);
item->setBackground(
(listWidget->count() % 2)?
Qt::white :
Qt::lightGray
);
QVariant data;
data.setValue(entry);
item->setData(Qt::UserRole, data);
return item;
}
void ListWidgetHandler::forward(const std::vector<TraceEntry> &entryVec)
{
for (unsigned int i = 0; i < entryVec.size(); ++i)
{
handleEvent(entryVec[i]);
}
}
void ListWidgetHandler::rewind(unsigned int count)
{
while (count--)
{
undoEvent();
}
}
} // namespace Templar