Diffstat (limited to 'noncore/apps/tinykate/libkate/view/kateundohistory.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/tinykate/libkate/view/kateundohistory.cpp | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/view/kateundohistory.cpp b/noncore/apps/tinykate/libkate/view/kateundohistory.cpp new file mode 100644 index 0000000..b7b9b56 --- a/dev/null +++ b/noncore/apps/tinykate/libkate/view/kateundohistory.cpp | |||
@@ -0,0 +1,271 @@ | |||
1 | /* | ||
2 | Copyright (C) 1999 Glen Parker <glenebob@nwlink.com> | ||
3 | Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | |||
20 | -------------------------------------------------------------------- | ||
21 | |||
22 | This implements a dialog used to display and control undo/redo history. | ||
23 | It uses a specialized QListBox subclass to provide a selection mechanism | ||
24 | that must: | ||
25 | 1) always have the first item selected, and | ||
26 | 2) maintain a contiguous multiple selection | ||
27 | */ | ||
28 | |||
29 | #include <stdio.h> | ||
30 | |||
31 | #include <qwidget.h> | ||
32 | #include <qdialog.h> | ||
33 | #include <qlayout.h> | ||
34 | #include <qlabel.h> | ||
35 | #include <qlistbox.h> | ||
36 | #include <qpushbutton.h> | ||
37 | |||
38 | #include <klocale.h> | ||
39 | |||
40 | #include "kateview.h" | ||
41 | |||
42 | #include "kateundohistory.h" | ||
43 | |||
44 | ///////////////////////////////////////////////////////////////////// | ||
45 | // UndoHistory implementation | ||
46 | // | ||
47 | UndoHistory::UndoHistory(KateView *kWrite, QWidget *parent, const char *name, bool modal, WFlags f) | ||
48 | : QDialog(parent, name, modal, f) | ||
49 | { | ||
50 | this->kWrite = kWrite; | ||
51 | |||
52 | QPushButton *btn; | ||
53 | QLabel *lbl; | ||
54 | QHBoxLayout *hLayout; | ||
55 | QVBoxLayout *vLayout; | ||
56 | |||
57 | hLayout = new QHBoxLayout(this, 5, 4); | ||
58 | |||
59 | vLayout = new QVBoxLayout(hLayout); | ||
60 | lbl = new QLabel(i18n("Undo List"), this); | ||
61 | lbUndo = new UndoListBox(this); | ||
62 | vLayout->addWidget(lbl); | ||
63 | vLayout->addWidget(lbUndo); | ||
64 | |||
65 | vLayout = new QVBoxLayout(hLayout); | ||
66 | lbl = new QLabel(i18n("Redo List"), this); | ||
67 | lbRedo = new UndoListBox(this); | ||
68 | vLayout->addWidget(lbl); | ||
69 | vLayout->addWidget(lbRedo); | ||
70 | |||
71 | lbUndo->setMinimumSize(QSize(150,140)); | ||
72 | lbRedo->setMinimumSize(QSize(150,140)); | ||
73 | |||
74 | connect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int))); | ||
75 | connect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int))); | ||
76 | |||
77 | vLayout = new QVBoxLayout(hLayout); | ||
78 | |||
79 | btnUndo = new QPushButton(this); | ||
80 | btnUndo->setText(i18n("&Undo")); | ||
81 | btnUndo->setEnabled(false); | ||
82 | btnUndo->setFixedSize(btnUndo->sizeHint()); | ||
83 | connect(btnUndo, SIGNAL(clicked()), this, SLOT(slotUndo())); | ||
84 | |||
85 | vLayout->addWidget(btnUndo, 0); | ||
86 | |||
87 | btnRedo = new QPushButton(this); | ||
88 | btnRedo->setText(i18n("&Redo")); | ||
89 | btnRedo->setEnabled(false); | ||
90 | btnRedo->setFixedSize(btnRedo->sizeHint()); | ||
91 | connect(btnRedo, SIGNAL(clicked()), this, SLOT(slotRedo())); | ||
92 | |||
93 | vLayout->addWidget(btnRedo, 0); | ||
94 | |||
95 | btn = new QPushButton(this); | ||
96 | btn->setText(i18n("&Close")); | ||
97 | btn->setFixedSize(btn->sizeHint()); | ||
98 | connect(btn, SIGNAL(clicked()), this, SLOT(close())); | ||
99 | |||
100 | vLayout->addWidget(btn, 0, AlignBottom); | ||
101 | |||
102 | newUndo(); | ||
103 | } | ||
104 | |||
105 | UndoHistory::~UndoHistory() | ||
106 | {} | ||
107 | |||
108 | void UndoHistory::newUndo() | ||
109 | { | ||
110 | QValueList<int> undoList; | ||
111 | QValueList<int>::Iterator it; | ||
112 | |||
113 | // we don't want a signal storm... | ||
114 | disconnect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int))); | ||
115 | disconnect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int))); | ||
116 | |||
117 | kWrite->undoTypeList(undoList); | ||
118 | |||
119 | lbUndo->clear(); | ||
120 | |||
121 | for (it = undoList.begin() ; it != undoList.end() ; it++) { | ||
122 | lbUndo->insertItem(i18n(kWrite->undoTypeName(*it))); | ||
123 | } | ||
124 | |||
125 | kWrite->redoTypeList(undoList); | ||
126 | |||
127 | lbRedo->clear(); | ||
128 | for (it = undoList.begin() ; it != undoList.end() ; it++) { | ||
129 | lbRedo->insertItem(i18n(kWrite->undoTypeName(*it))); | ||
130 | } | ||
131 | |||
132 | connect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int))); | ||
133 | connect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int))); | ||
134 | |||
135 | slotUndoSelChanged(lbUndo->selCount()); | ||
136 | slotRedoSelChanged(lbRedo->selCount()); | ||
137 | } | ||
138 | |||
139 | void UndoHistory::slotUndo() | ||
140 | { | ||
141 | int selCount = lbUndo->selCount(); | ||
142 | emit undo(selCount); | ||
143 | lbRedo->setSelCount(selCount); | ||
144 | } | ||
145 | void UndoHistory::slotRedo() | ||
146 | { | ||
147 | int selCount = lbRedo->selCount(); | ||
148 | emit redo(selCount); | ||
149 | lbUndo->setSelCount(selCount); | ||
150 | } | ||
151 | |||
152 | void UndoHistory::slotUndoSelChanged(int cnt) | ||
153 | { | ||
154 | btnUndo->setEnabled(cnt > 0); | ||
155 | } | ||
156 | |||
157 | void UndoHistory::slotRedoSelChanged(int cnt) | ||
158 | { | ||
159 | btnRedo->setEnabled(cnt > 0); | ||
160 | } | ||
161 | |||
162 | ///////////////////////////////////////////////////////////////////// | ||
163 | // UndoListBox implementation | ||
164 | // | ||
165 | UndoListBox::UndoListBox(QWidget *parent, const char *name, WFlags f) | ||
166 | : QListBox(parent, name, f) | ||
167 | { | ||
168 | _selCount = 0; | ||
169 | setSelectionMode(Extended); | ||
170 | connect(this, SIGNAL(highlighted(int)), this, SLOT(_slotSelectionChanged())); | ||
171 | connect(this, SIGNAL(selectionChanged()), this, SLOT(_slotSelectionChanged())); | ||
172 | } | ||
173 | |||
174 | UndoListBox::~UndoListBox() | ||
175 | {} | ||
176 | |||
177 | void UndoListBox::insertItem (const QString &text, int index) | ||
178 | { | ||
179 | bool sig = false; | ||
180 | |||
181 | if (count() == 0) | ||
182 | sig = true; | ||
183 | else if (index > -1) | ||
184 | sig = (isSelected(index)); | ||
185 | |||
186 | QListBox::insertItem(text, index); | ||
187 | |||
188 | if (sig) | ||
189 | _slotSelectionChanged(); | ||
190 | } | ||
191 | |||
192 | void UndoListBox::removeItem (int index) | ||
193 | { | ||
194 | bool sig; | ||
195 | |||
196 | if (count() == 1) | ||
197 | sig = true; | ||
198 | else if (index == -1) | ||
199 | sig = (isSelected(count() - 1)); | ||
200 | else | ||
201 | sig = (isSelected(index)); | ||
202 | |||
203 | QListBox::removeItem(index); | ||
204 | |||
205 | if (sig) | ||
206 | _slotSelectionChanged(); | ||
207 | } | ||
208 | |||
209 | void UndoListBox::clear() | ||
210 | { | ||
211 | bool sig = (count() > 0); | ||
212 | |||
213 | QListBox::clear(); | ||
214 | |||
215 | if (sig) | ||
216 | _slotSelectionChanged(); | ||
217 | } | ||
218 | |||
219 | int UndoListBox::selCount() | ||
220 | { | ||
221 | return _selCount; | ||
222 | } | ||
223 | |||
224 | void UndoListBox::setSelCount(int count) | ||
225 | { | ||
226 | if (count == _selCount) | ||
227 | return; | ||
228 | |||
229 | if (count < 1 || count > (int)this->count()) | ||
230 | return; | ||
231 | |||
232 | setCurrentItem(count - 1); | ||
233 | } | ||
234 | |||
235 | // make sure the first item is selected, and that there are no holes | ||
236 | void UndoListBox::_slotSelectionChanged() | ||
237 | { | ||
238 | int count = this->count(); | ||
239 | |||
240 | if (! count) { | ||
241 | if (_selCount != 0) { | ||
242 | _selCount = 0; | ||
243 | emit sigSelected(_selCount); | ||
244 | } | ||
245 | return; | ||
246 | } | ||
247 | |||
248 | if (currentItem() < 0) | ||
249 | setCurrentItem(0); | ||
250 | |||
251 | int i; | ||
252 | int currItem = currentItem(); | ||
253 | int max = (currItem+1 > _selCount ? currItem+1 : _selCount); | ||
254 | |||
255 | for (i = 0 ; i < max ; i++) { | ||
256 | if (i > currItem) { | ||
257 | if (isSelected(i)) { | ||
258 | setSelected(i, false); | ||
259 | } | ||
260 | } else { | ||
261 | if (! isSelected(i)) { | ||
262 | setSelected(i, true); | ||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | if (_selCount != currItem + 1) { | ||
268 | _selCount = currItem + 1; | ||
269 | emit sigSelected(_selCount); | ||
270 | } | ||
271 | } | ||