1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : QAD_DirListDlg.cxx
8 // Author : Vadim SANDLER
12 #include "QAD_DirListDlg.h"
13 #include "QAD_Desktop.h"
14 #include "QAD_FileDlg.h"
15 #include "QAD_MessageBox.h"
16 #include "QAD_Tools.h"
19 #include <qpushbutton.h>
20 #include <qapplication.h>
24 #define MIN_HEIGHT 200
25 #define MARGIN_SIZE 11
26 #define SPACING_SIZE 6
32 QAD_DirListDlg::QAD_DirListDlg(QWidget* parent, const char* name)
33 : QDialog (parent, name, true, WStyle_NormalBorder | WStyle_Customize | WStyle_Title | WStyle_SysMenu )
40 setCaption(tr("QUICK_DIR_LIST_TLT"));
41 setSizeGripEnabled( true );
43 QGridLayout* topLayout = new QGridLayout(this);
44 topLayout->setMargin(MARGIN_SIZE);
45 topLayout->setSpacing(SPACING_SIZE);
47 myDirList = new QListBox(this);
48 myDirList->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
49 // myDirList->setMaximumSize(MIN_WIDTH, MIN_HEIGHT);
50 myDirList->setSelectionMode(QListBox::Single);
51 myDirList->setHScrollBarMode(QListBox::AlwaysOff);
52 myDirList->horizontalScrollBar()->installEventFilter(this);
53 myDirList->verticalScrollBar()->installEventFilter(this);
54 myDirList->insertItem(tr("EMPTY_DIR"));
55 myDirList->installEventFilter(this);
57 QHBoxLayout* ctrlLayout = new QHBoxLayout;
58 ctrlLayout->setMargin(0);
59 ctrlLayout->setSpacing(0);
61 QLabel* lab = new QLabel(myDirList, tr("DIRECTORIES_LBL"), this);
63 QToolButton* insertBtn = new QToolButton(this);
64 insertBtn->setIconSet(QAD_Desktop::getResourceManager()->loadPixmap("QAD", tr("ICON_DIRLIST_INSERT")));
65 insertBtn->setAutoRaise(true);
67 QToolButton* deleteBtn = new QToolButton(this);
68 deleteBtn->setIconSet(QAD_Desktop::getResourceManager()->loadPixmap("QAD", tr("ICON_DIRLIST_DELETE")));
69 deleteBtn->setAutoRaise(true);
71 QToolButton* upBtn = new QToolButton(this);
72 upBtn->setIconSet(QAD_Desktop::getResourceManager()->loadPixmap("QAD", tr("ICON_DIRLIST_MOVEUP")));
73 upBtn->setAutoRaise(true);
75 QToolButton* downBtn = new QToolButton(this);
76 downBtn->setIconSet(QAD_Desktop::getResourceManager()->loadPixmap("QAD", tr("ICON_DIRLIST_MOVEDOWN")));
77 downBtn->setAutoRaise(true);
79 ctrlLayout->addWidget(lab);
80 ctrlLayout->addItem(new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum));
81 ctrlLayout->addWidget(insertBtn);
82 ctrlLayout->addWidget(deleteBtn);
83 ctrlLayout->addWidget(upBtn);
84 ctrlLayout->addWidget(downBtn);
86 QHBoxLayout* btnLayout = new QHBoxLayout;
87 btnLayout->setMargin(0);
88 btnLayout->setSpacing(6);
90 QPushButton * okBtn = new QPushButton(tr("BUT_OK"), this);
91 QPushButton * cancelBtn = new QPushButton(tr("BUT_CANCEL"), this);
92 okBtn->setDefault(true);
93 okBtn->setAutoDefault(true);
94 cancelBtn->setAutoDefault(true);
96 btnLayout->addWidget(okBtn);
97 btnLayout->addItem(new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum));
98 btnLayout->addWidget(cancelBtn);
100 topLayout->addLayout(ctrlLayout, 0, 0);
101 topLayout->addWidget(myDirList, 1, 0);
102 topLayout->addLayout(btnLayout, 2, 0);
104 connect(myDirList, SIGNAL(mouseButtonClicked(int, QListBoxItem*, const QPoint&)),
105 this, SLOT(onMouseButtonClicked(int, QListBoxItem*, const QPoint&)));
106 connect(myDirList, SIGNAL(doubleClicked(QListBoxItem*)),
107 this, SLOT(onDblClicked(QListBoxItem*)));
109 connect(insertBtn, SIGNAL(clicked()), this, SLOT(onInsert()));
110 connect(deleteBtn, SIGNAL(clicked()), this, SLOT(onDelete()));
111 connect(upBtn, SIGNAL(clicked()), this, SLOT(onUp()));
112 connect(downBtn, SIGNAL(clicked()), this, SLOT(onDown()));
113 connect(okBtn, SIGNAL(clicked()), this, SLOT(onOk()));
114 connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
116 QAD_Tools::centerWidget(this, parent);
122 QAD_DirListDlg::~QAD_DirListDlg() {
128 void QAD_DirListDlg::getPathList(QStringList& list) {
130 for (unsigned i = 0; i < myDirList->count()-1; i++)
131 list.append(myDirList->text(i));
137 void QAD_DirListDlg::setPathList(const QStringList& list) {
138 for (unsigned i = 0; i < list.count(); i++)
139 myDirList->insertItem(list[i], myDirList->count()-1);
143 Validates entered path, returns true if OK
145 bool QAD_DirListDlg::validate() {
147 QString dirPath = myEdit->text().stripWhiteSpace();
149 QListBoxItem* found = 0;
150 for (unsigned i = 0; i < myDirList->count()-1; i++) {
151 QDir aDir(myDirList->text(i));
152 if ( aDir.canonicalPath().isNull() && myDirList->text(i) == dir.absPath() ||
153 !aDir.canonicalPath().isNull() && aDir.exists() && aDir.canonicalPath() == dir.canonicalPath()) {
154 found = myDirList->item(i);
158 if (dirPath.isEmpty()) {
160 // it should be last (empty) item in the list - nothing to do
164 // delete directory from the list
165 removeDir(myLastSelected);
171 if (found != myLastSelected) {
172 // it is forbidden to add directory more then once
173 QAD_MessageBox::error1(this,
175 tr("ERR_DIRECTORY_SPECIFIED"),
183 if ( QAD_MessageBox::info2(this,
185 tr("WRN_DIRECTORY_N0T_EXIST").arg(dir.absPath()),
186 tr ("BUT_YES"), tr ("BUT_NO"),
187 QAD_YES, QAD_NO, QAD_NO ) == QAD_NO ) {
193 appendDir(myLastSelected, dir.absPath());
201 Appends/changes directory
203 void QAD_DirListDlg::appendDir(QListBoxItem* item, const QString& dir) {
204 int index = myDirList->index(item);
205 if (index >= 0 && index < (int)myDirList->count()) {
206 if (index == (int)myDirList->count()-1) {
207 // it is the last item (new), well, insert it before the last (empty)
208 myDirList->insertItem(dir, myDirList->count()-1);
212 myDirList->changeItem(dir, index);
218 Removes directory from list
220 void QAD_DirListDlg::removeDir(QListBoxItem* item) {
221 // do not remove last item (empty)
222 int index = myDirList->index(item);
223 if (index >= 0 && index < (int)myDirList->count()-1) {
225 myLastSelected = myDirList->item(index);
226 myDirList->setSelected(myLastSelected, true);
231 KeyPress event handler, processes <Enter> and <Escape> keys
233 void QAD_DirListDlg::keyPressEvent(QKeyEvent* event) {
235 if ( event->key() == Key_Escape ) {
241 myDirList->setFocus();
245 if (event->key() == Key_Return || event->key() == Key_Enter) {
248 QDialog::keyPressEvent(event);
254 void QAD_DirListDlg::resizeEvent(QResizeEvent* event) {
255 QDialog::resizeEvent(event);
257 myEdit->resize(myDirList->viewport()->width()-myBtn->sizeHint().width(), myEdit->height());
258 myBtn->move(myEdit->width(), myEdit->y());
263 Called when user clicks inside directories list box
265 void QAD_DirListDlg::onMouseButtonClicked(int button,
267 const QPoint& point) {
270 myDirList->setCurrentItem(myLastSelected);
271 myDirList->setSelected(myLastSelected, true);
279 myDirList->setFocus();
282 myDirList->setCurrentItem(item);
283 myDirList->setSelected(item, true);
284 myDirList->ensureCurrentVisible();
285 qApp->processEvents();
286 if (button == LeftButton && myLastSelected == item) {
287 QRect ir = myDirList->itemRect(myLastSelected);
289 myEdit = new QLineEdit(myDirList->viewport());
290 myBtn = new QToolButton(myDirList->viewport());
291 myBtn->setText(" ... ");
292 connect(myBtn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
293 myEdit->setGeometry(0,
294 ir.top()-(myEdit->sizeHint().height()-ir.height())/2,
295 myDirList->viewport()->width()-myBtn->sizeHint().width(),
296 myEdit->sizeHint().height());
297 myBtn->setGeometry (myEdit->width(),
298 ir.top()-(myEdit->sizeHint().height()-ir.height())/2,
299 myBtn->sizeHint().width(),
300 myEdit->sizeHint().height());
301 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditFinished()));
305 if (myDirList->index(myLastSelected) != (int)myDirList->count()-1)
306 myEdit->setText(myLastSelected->text());
308 myEdit->setCursorPosition(myEdit->text().length());
309 myEdit->installEventFilter(this);
314 myDirList->clearSelection();
316 myLastSelected = item;
320 Called when user double-clicks on any item
322 void QAD_DirListDlg::onDblClicked(QListBoxItem* item) {
323 onMouseButtonClicked(LeftButton, item, QPoint(0,0));
327 <...> (Browse dir) button slot
329 void QAD_DirListDlg::onBtnClicked() {
330 QString dir = myEdit->text().stripWhiteSpace().isEmpty() ?
332 myEdit->text().stripWhiteSpace();
333 dir = QAD_FileDlg::getExistingDirectory(this, dir, tr("SELECT_DIRECTORY"), true);
334 if (!dir.isEmpty()) {
335 myEdit->setText(dir);
337 myEdit->setCursorPosition(myEdit->text().length());
342 Called when user finises editing of path by pressing <Enter>
344 void QAD_DirListDlg::onEditFinished() {
347 myDirList->setCurrentItem(myLastSelected);
348 myDirList->setSelected(myLastSelected, true);
356 myDirList->setFocus();
363 bool QAD_DirListDlg::eventFilter(QObject* object, QEvent* event) {
365 if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) {
366 if (object == myDirList->horizontalScrollBar() || object == myDirList->verticalScrollBar()) {
368 myDirList->setCurrentItem(myLastSelected);
369 myDirList->setSelected(myLastSelected, true);
377 myDirList->setFocus();
380 else if (event->type() == QEvent::KeyPress) {
381 QKeyEvent* ke = (QKeyEvent*)event;
382 if (ke->key() == Key_Tab)
384 if (object == myDirList) {
387 else if (object == myEdit) {
388 if ( ke->key() == Key_Up || ke->key() == Key_Down || ke->key() == Key_PageUp || ke->key() == Key_PageDown ||
389 ( ke->key() == Key_Home || ke->key() == Key_End || ke->key() == Key_Prior || ke->key() == Key_Next ) &&
390 (ke->state() & ControlButton) ) {
393 else if ( ke->key() == Key_Escape ) {
399 myDirList->setFocus();
406 if (event->type() == QEvent::KeyPress) {
407 if (object == myDirList) {
408 QKeyEvent* ke = (QKeyEvent*)event;
409 if (ke->key() == Key_Return || ke->key() == Key_Enter) {
413 QChar c(ke->ascii());
415 QListBoxItem* item = myDirList->item(myDirList->currentItem());
417 myDirList->setCurrentItem(item);
418 myDirList->setSelected(item, true);
419 myDirList->ensureCurrentVisible();
420 myLastSelected = item;
421 qApp->processEvents();
422 QRect ir = myDirList->itemRect(myLastSelected);
424 myEdit = new QLineEdit(myDirList->viewport());
425 myBtn = new QToolButton(myDirList->viewport());
426 myBtn->setText(" ... ");
427 connect(myBtn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
428 myEdit->setGeometry(0,
429 ir.top()-(myEdit->sizeHint().height()-ir.height())/2,
430 myDirList->viewport()->width()-myBtn->sizeHint().width(),
431 myEdit->sizeHint().height());
432 myBtn->setGeometry (myEdit->width(),
433 ir.top()-(myEdit->sizeHint().height()-ir.height())/2,
434 myBtn->sizeHint().width(),
435 myEdit->sizeHint().height());
436 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditFinished()));
441 myEdit->setCursorPosition(myEdit->text().length());
442 myEdit->installEventFilter(this);
450 return QDialog::eventFilter(object, event);
456 void QAD_DirListDlg::onInsert() {
459 onMouseButtonClicked(LeftButton, myDirList->item(myDirList->count()-1), QPoint(0,0));
460 onMouseButtonClicked(LeftButton, myDirList->item(myDirList->count()-1), QPoint(0,0));
467 void QAD_DirListDlg::onDelete() {
468 if (!myEdited && myDirList->currentItem() >=0) {
469 removeDir(myDirList->item(myDirList->currentItem()));
470 myDirList->setFocus();
475 <Move up> button slot
477 void QAD_DirListDlg::onUp() {
478 if (!myEdited && myLastSelected) {
479 int index = myDirList->currentItem();
480 if (index > 0 && index < (int)myDirList->count()-1 && myDirList->isSelected(index)) {
481 QString t = myDirList->text(index-1);
482 myDirList->changeItem(myDirList->text(index), index-1);
483 myDirList->changeItem(t, index);
484 myDirList->setCurrentItem(index-1);
485 myLastSelected = myDirList->item(index-1);
486 myDirList->setSelected(myLastSelected, true);
487 myDirList->setFocus();
493 <Move down> button slot
495 void QAD_DirListDlg::onDown() {
496 if (!myEdited && myLastSelected) {
497 int index = myDirList->currentItem();
498 if (index >= 0 && index < (int)myDirList->count()-2 && myDirList->isSelected(index)) {
499 QString t = myDirList->text(index+1);
500 myDirList->changeItem(myDirList->text(index), index+1);
501 myDirList->changeItem(t, index);
502 myDirList->setCurrentItem(index+1);
503 myLastSelected = myDirList->item(index+1);
504 myDirList->setSelected(myLastSelected, true);
505 myDirList->setFocus();
511 Purpose : <OK> button slot
513 void QAD_DirListDlg::onOk() {