00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030
00031 #include <qwidget.h>
00032 #include <qdialog.h>
00033 #include <qlayout.h>
00034 #include <qlabel.h>
00035 #include <qlistbox.h>
00036 #include <qpushbutton.h>
00037
00038 #include <klocale.h>
00039
00040 #include "kateview.h"
00041
00042 #include "kateundohistory.h"
00043
00045
00046
00047 UndoHistory::UndoHistory(KateView *kWrite, QWidget *parent, const char *name, bool modal, WFlags f)
00048 : QDialog(parent, name, modal, f)
00049 {
00050 this->kWrite = kWrite;
00051
00052 QPushButton *btn;
00053 QLabel *lbl;
00054 QHBoxLayout *hLayout;
00055 QVBoxLayout *vLayout;
00056
00057 hLayout = new QHBoxLayout(this, 5, 4);
00058
00059 vLayout = new QVBoxLayout(hLayout);
00060 lbl = new QLabel(i18n("Undo List"), this);
00061 lbUndo = new UndoListBox(this);
00062 vLayout->addWidget(lbl);
00063 vLayout->addWidget(lbUndo);
00064
00065 vLayout = new QVBoxLayout(hLayout);
00066 lbl = new QLabel(i18n("Redo List"), this);
00067 lbRedo = new UndoListBox(this);
00068 vLayout->addWidget(lbl);
00069 vLayout->addWidget(lbRedo);
00070
00071 lbUndo->setMinimumSize(QSize(150,140));
00072 lbRedo->setMinimumSize(QSize(150,140));
00073
00074 connect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int)));
00075 connect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int)));
00076
00077 vLayout = new QVBoxLayout(hLayout);
00078
00079 btnUndo = new QPushButton(this);
00080 btnUndo->setText(i18n("&Undo"));
00081 btnUndo->setEnabled(false);
00082 btnUndo->setFixedSize(btnUndo->sizeHint());
00083 connect(btnUndo, SIGNAL(clicked()), this, SLOT(slotUndo()));
00084
00085 vLayout->addWidget(btnUndo, 0);
00086
00087 btnRedo = new QPushButton(this);
00088 btnRedo->setText(i18n("&Redo"));
00089 btnRedo->setEnabled(false);
00090 btnRedo->setFixedSize(btnRedo->sizeHint());
00091 connect(btnRedo, SIGNAL(clicked()), this, SLOT(slotRedo()));
00092
00093 vLayout->addWidget(btnRedo, 0);
00094
00095 btn = new QPushButton(this);
00096 btn->setText(i18n("&Close"));
00097 btn->setFixedSize(btn->sizeHint());
00098 connect(btn, SIGNAL(clicked()), this, SLOT(close()));
00099
00100 vLayout->addWidget(btn, 0, AlignBottom);
00101
00102 newUndo();
00103 }
00104
00105 UndoHistory::~UndoHistory()
00106 {}
00107
00108 void UndoHistory::newUndo()
00109 {
00110 QValueList<int> undoList;
00111 QValueList<int>::Iterator it;
00112
00113
00114 disconnect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int)));
00115 disconnect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int)));
00116
00117 kWrite->undoTypeList(undoList);
00118
00119 lbUndo->clear();
00120
00121 for (it = undoList.begin() ; it != undoList.end() ; it++) {
00122 lbUndo->insertItem(i18n(kWrite->undoTypeName(*it)));
00123 }
00124
00125 kWrite->redoTypeList(undoList);
00126
00127 lbRedo->clear();
00128 for (it = undoList.begin() ; it != undoList.end() ; it++) {
00129 lbRedo->insertItem(i18n(kWrite->undoTypeName(*it)));
00130 }
00131
00132 connect(lbUndo, SIGNAL(sigSelected(int)), this, SLOT(slotUndoSelChanged(int)));
00133 connect(lbRedo, SIGNAL(sigSelected(int)), this, SLOT(slotRedoSelChanged(int)));
00134
00135 slotUndoSelChanged(lbUndo->selCount());
00136 slotRedoSelChanged(lbRedo->selCount());
00137 }
00138
00139 void UndoHistory::slotUndo()
00140 {
00141 int selCount = lbUndo->selCount();
00142 emit undo(selCount);
00143 lbRedo->setSelCount(selCount);
00144 }
00145 void UndoHistory::slotRedo()
00146 {
00147 int selCount = lbRedo->selCount();
00148 emit redo(selCount);
00149 lbUndo->setSelCount(selCount);
00150 }
00151
00152 void UndoHistory::slotUndoSelChanged(int cnt)
00153 {
00154 btnUndo->setEnabled(cnt > 0);
00155 }
00156
00157 void UndoHistory::slotRedoSelChanged(int cnt)
00158 {
00159 btnRedo->setEnabled(cnt > 0);
00160 }
00161
00163
00164
00165 UndoListBox::UndoListBox(QWidget *parent, const char *name, WFlags f)
00166 : QListBox(parent, name, f)
00167 {
00168 _selCount = 0;
00169 setSelectionMode(Extended);
00170 connect(this, SIGNAL(highlighted(int)), this, SLOT(_slotSelectionChanged()));
00171 connect(this, SIGNAL(selectionChanged()), this, SLOT(_slotSelectionChanged()));
00172 }
00173
00174 UndoListBox::~UndoListBox()
00175 {}
00176
00177 void UndoListBox::insertItem (const QString &text, int index)
00178 {
00179 bool sig = false;
00180
00181 if (count() == 0)
00182 sig = true;
00183 else if (index > -1)
00184 sig = (isSelected(index));
00185
00186 QListBox::insertItem(text, index);
00187
00188 if (sig)
00189 _slotSelectionChanged();
00190 }
00191
00192 void UndoListBox::removeItem (int index)
00193 {
00194 bool sig;
00195
00196 if (count() == 1)
00197 sig = true;
00198 else if (index == -1)
00199 sig = (isSelected(count() - 1));
00200 else
00201 sig = (isSelected(index));
00202
00203 QListBox::removeItem(index);
00204
00205 if (sig)
00206 _slotSelectionChanged();
00207 }
00208
00209 void UndoListBox::clear()
00210 {
00211 bool sig = (count() > 0);
00212
00213 QListBox::clear();
00214
00215 if (sig)
00216 _slotSelectionChanged();
00217 }
00218
00219 int UndoListBox::selCount()
00220 {
00221 return _selCount;
00222 }
00223
00224 void UndoListBox::setSelCount(int count)
00225 {
00226 if (count == _selCount)
00227 return;
00228
00229 if (count < 1 || count > (int)this->count())
00230 return;
00231
00232 setCurrentItem(count - 1);
00233 }
00234
00235
00236 void UndoListBox::_slotSelectionChanged()
00237 {
00238 int count = this->count();
00239
00240 if (! count) {
00241 if (_selCount != 0) {
00242 _selCount = 0;
00243 emit sigSelected(_selCount);
00244 }
00245 return;
00246 }
00247
00248 if (currentItem() < 0)
00249 setCurrentItem(0);
00250
00251 int i;
00252 int currItem = currentItem();
00253 int max = (currItem+1 > _selCount ? currItem+1 : _selCount);
00254
00255 for (i = 0 ; i < max ; i++) {
00256 if (i > currItem) {
00257 if (isSelected(i)) {
00258 setSelected(i, false);
00259 }
00260 } else {
00261 if (! isSelected(i)) {
00262 setSelected(i, true);
00263 }
00264 }
00265 }
00266
00267 if (_selCount != currItem + 1) {
00268 _selCount = currItem + 1;
00269 emit sigSelected(_selCount);
00270 }
00271 }