]> git.sven.stormbind.net Git - sven/vym.git/blob - src/historywindow.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / historywindow.cpp
1 #include "historywindow.h"
2 #include "mainwindow.h"
3
4 extern Settings settings;
5 extern Main *mainWindow;
6
7 HistoryWindow::HistoryWindow(QWidget *parent) : QDialog(parent)
8 {
9     ui.setupUi(this);
10     ui.historyTable->setRowCount(
11         settings.value("/history/stepsTotal", 75).toInt());
12     ui.historyTable->setColumnCount(3);
13
14     QTableWidgetItem *item;
15
16     item = new QTableWidgetItem(tr("Action", "Table with actions"));
17     ui.historyTable->setHorizontalHeaderItem(0, item);
18
19     item = new QTableWidgetItem(tr("Comment", "Table with actions"));
20     ui.historyTable->setHorizontalHeaderItem(1, item);
21
22     item = new QTableWidgetItem(tr("Undo action", "Table with actions"));
23     ui.historyTable->setHorizontalHeaderItem(2, item);
24
25     ui.historyTable->setSelectionBehavior(QAbstractItemView::SelectRows);
26
27     ui.undoButton->setIcon(QIcon(":/undo.png"));
28     ui.redoButton->setIcon(QIcon(":/redo.png"));
29
30     connect(ui.undoButton, SIGNAL(clicked()), this, SLOT(undo()));
31     connect(ui.redoButton, SIGNAL(clicked()), this, SLOT(redo()));
32     connect(ui.historyTable, SIGNAL(itemSelectionChanged()), this,
33             SLOT(select()));
34
35     // Load Settings
36
37     resize(
38         settings
39             .value("/satellite/historywindow/geometry/size", QSize(1000, 400))
40             .toSize());
41     move(settings.value("/satellite/historywindow/geometry/pos", QPoint(0, 450))
42              .toPoint());
43
44     ui.historyTable->setColumnWidth(
45         0,
46         settings.value("/satellite/historywindow/geometry/columnWidth/0", 250)
47             .toInt());
48     ui.historyTable->setColumnWidth(
49         1,
50         settings.value("/satellite/historywindow/geometry/columnWidth/1", 350)
51             .toInt());
52     ui.historyTable->setColumnWidth(
53         2,
54         settings.value("/satellite/historywindow/geometry/columnWidth/2", 250)
55             .toInt());
56 }
57
58 HistoryWindow::~HistoryWindow()
59 {
60     // Save settings
61     settings.setValue("/satellite/historywindow/geometry/size", size());
62     settings.setValue("/satellite/historywindow/geometry/pos", pos());
63
64     for (int i = 0; i < 3; ++i)
65         settings.setValue(
66             QString("/satellite/historywindow/geometry/columnWidth/%1").arg(i),
67             ui.historyTable->columnWidth(i));
68 }
69
70 void HistoryWindow::clearRow(int row)
71 {
72     QTableWidgetItem *it;
73     it = ui.historyTable->item(row, 0);
74     if (it)
75         it->setText("");
76     it = ui.historyTable->item(row, 1);
77     if (it)
78         it->setText("");
79     it = ui.historyTable->item(row, 2);
80     if (it)
81         it->setText("");
82 }
83
84 void HistoryWindow::updateRow(int row, int step, SimpleSettings &set)
85 {
86     QTableWidgetItem *item;
87
88     item = new QTableWidgetItem(
89         set.value(QString("/history/step-%1/redoCommand").arg(step)));
90     ui.historyTable->setItem(row, 0, item);
91
92     item = new QTableWidgetItem(
93         set.value(QString("/history/step-%1/comment").arg(step)));
94     ui.historyTable->setItem(row, 1, item);
95
96     item = new QTableWidgetItem(
97         set.value(QString("/history/step-%1/undoCommand").arg(step)));
98     ui.historyTable->setItem(row, 2, item);
99 }
100
101 void HistoryWindow::update(SimpleSettings &set)
102 {
103     int undosAvail = set.numValue("/history/undosAvail", 0);
104     int redosAvail = set.numValue("/history/redosAvail", 0);
105     int stepsTotal = set.numValue("/history/stepsTotal", 1000);
106     int curStep = set.numValue("/history/curStep");
107     int i;
108     int s = curStep;
109     int r = undosAvail - 1;
110     QTableWidgetItem *item;
111
112     // Update number of rows
113     ui.historyTable->setRowCount(undosAvail + redosAvail + 1);
114
115     // Update buttons
116     if (undosAvail < 1)
117         ui.undoButton->setEnabled(false);
118     else
119         ui.undoButton->setEnabled(true);
120
121     if (redosAvail < 1)
122         ui.redoButton->setEnabled(false);
123     else
124         ui.redoButton->setEnabled(true);
125
126     // Update undos in table
127     for (i = undosAvail; i > 0; i--) {
128         updateRow(r, s, set);
129         r--;
130         s--;
131         if (s < 1)
132             s = stepsTotal;
133     }
134
135     // Generated the "now" row
136     QColor c = palette().color(QPalette::Highlight);
137     for (i = 0; i <= 2; i++) {
138         if (i != 1) {
139             item = new QTableWidgetItem("");
140             item->setBackground(QBrush(c));
141             ui.historyTable->setItem(undosAvail, i, item);
142         }
143     }
144     item = new QTableWidgetItem(
145         " - " + tr("Current state", "Current bar in history hwindow") + " - ");
146     item->setBackground(QBrush(c));
147     ui.historyTable->setItem(undosAvail, 1, item);
148
149     // Show "now" row
150     ui.historyTable->scrollToItem(item);
151
152     // Update Redos in table
153     s = curStep;
154     s++;
155     if (s > stepsTotal)
156         s = 1;
157     for (i = 1; i <= redosAvail; i++) {
158         updateRow(undosAvail + i, s, set);
159         s++;
160         if (s > stepsTotal)
161             s = 1;
162     }
163
164     // Delete the rest
165     for (i = undosAvail + redosAvail + 1; i <= stepsTotal; i++)
166         clearRow(i);
167
168     // ui.historyTable->resizeColumnsToContents();
169 }
170
171 void HistoryWindow::setStepsTotal(int st)
172 {
173     // Number of steps + "current" bar
174     ui.historyTable->setRowCount(st + 1);
175 }
176
177 void HistoryWindow::closeEvent(QCloseEvent *ce)
178 {
179     ce->accept();
180     hide();
181     emit(windowClosed());
182 }
183
184 void HistoryWindow::undo() { mainWindow->editUndo(); }
185
186 void HistoryWindow::redo() { mainWindow->editRedo(); }
187
188 void HistoryWindow::select()
189 {
190     mainWindow->gotoHistoryStep(
191         ui.historyTable->row(ui.historyTable->selectedItems().first()));
192 }