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