From: mzn Date: Thu, 21 Jul 2005 09:33:05 +0000 (+0000) Subject: Add preference for quick directory list. X-Git-Tag: V3_0_1~27 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=fc680ab5c1dd717d632e3dc9d87d6feb58019ecf;p=modules%2Fgui.git Add preference for quick directory list. --- diff --git a/src/Qtx/Makefile.in b/src/Qtx/Makefile.in index ffcc7f2e7..eda7470b7 100755 --- a/src/Qtx/Makefile.in +++ b/src/Qtx/Makefile.in @@ -46,7 +46,8 @@ EXPORT_HEADERS= Qtx.h \ QtxListResourceEdit.h \ QtxWorkstack.h \ QtxResourceEdit.h \ - QtxListView.h + QtxListView.h \ + QtxDirListEditor.h # .po files to transform in .qm @@ -91,7 +92,8 @@ LIB_SRC= \ QtxListResourceEdit.cxx \ QtxResourceEdit.cxx \ QtxWorkstack.cxx \ - QtxListView.cxx + QtxListView.cxx \ + QtxDirListEditor.cxx LIB_MOC = \ QtxAction.h \ @@ -121,7 +123,8 @@ LIB_MOC = \ QtxWorkstackAction.h \ QtxWorkstack.h \ QtxListView.h \ - QtxListResourceEdit.h + QtxListResourceEdit.h \ + QtxDirListEditor.h RESOURCES_FILES = \ diff --git a/src/Qtx/QtxDirListEditor.cxx b/src/Qtx/QtxDirListEditor.cxx new file mode 100644 index 000000000..639ab1fe7 --- /dev/null +++ b/src/Qtx/QtxDirListEditor.cxx @@ -0,0 +1,539 @@ +#include + +#include +#include +#include +#include +#include +#include +using namespace std; + +#define MARGIN_SIZE 11 +#define SPACING_SIZE 6 +#define SPACER_SIZE 5 + +static const char *delete_icon[] = { +" 16 16 3 1", +"` c #810000", +" c none", +"# c #ffffff", +" ", +" ", +" ``# ``# ", +" ````# ``# ", +" ````# ``# ", +" ```# `# ", +" `````# ", +" ```# ", +" `````# ", +" ```# ``# ", +" ```# ``# ", +" ```# `# ", +" ```# `# ", +" `# `# ", +" ", +" " +}; + +static const char *insert_icon[] = { +" 16 16 5 1", +"` c #000000", +". c #ffff00", +"# c #9d9da1", +" c none", +"b c #ffffff", +" ", +" ", +" # #b #. ", +" # #.#.` ` ` ", +" .#.b#### ` ", +" ### .. ", +" . # .# ` ", +" #` #. ", +" # ` ", +" ` ", +" ` ", +" ` ", +" ` ", +" ` ` ` ` ` ` ", +" ", +" " +}; + +static const char *movedown_icon[] = { +" 16 16 2 1", +"` c #000000", +" c none", +" ", +" ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ``````````` ", +" ````````` ", +" ``````` ", +" ````` ", +" ``` ", +" ` ", +" ", +" " +}; + +static const char *moveup_icon[] = { +" 16 16 2 1", +"` c #000000", +" c none", +" ", +" ", +" ` ", +" ``` ", +" ````` ", +" ``````` ", +" ````````` ", +" ``````````` ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ``` ", +" ", +" " +}; + + +/*! + Constructor +*/ +QtxDirListEditor::QtxDirListEditor(QWidget* parent) + : QWidget(parent) +{ + myEdited = false; + myLastSelected = 0; + myEdit = 0; + myBtn = 0; + + QGridLayout* topLayout = new QGridLayout(this); + topLayout->setMargin(0); + topLayout->setSpacing(0); + + setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); + + myDirList = new QListBox(this); + myDirList->setSelectionMode(QListBox::Single); + myDirList->setHScrollBarMode(QListBox::AlwaysOff); + myDirList->horizontalScrollBar()->installEventFilter(this); + myDirList->verticalScrollBar()->installEventFilter(this); + myDirList->insertItem(tr("")); + myDirList->installEventFilter(this); + + QHBoxLayout* ctrlLayout = new QHBoxLayout; + ctrlLayout->setMargin(0); + ctrlLayout->setSpacing(0); + + // QLabel* lab = new QLabel(myDirList, tr("DIRECTORIES_LBL"), this); + + QToolButton* insertBtn = new QToolButton(this); + insertBtn->setIconSet(QPixmap( insert_icon )); + insertBtn->setAutoRaise(true); + + QToolButton* deleteBtn = new QToolButton(this); + deleteBtn->setIconSet(QPixmap( delete_icon )); + deleteBtn->setAutoRaise(true); + + QToolButton* upBtn = new QToolButton(this); + upBtn->setIconSet(QPixmap( moveup_icon )); + upBtn->setAutoRaise(true); + + QToolButton* downBtn = new QToolButton(this); + downBtn->setIconSet(QPixmap( movedown_icon )); + downBtn->setAutoRaise(true); + + // ctrlLayout->addWidget(lab); + ctrlLayout->addItem(new QSpacerItem(SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum)); + ctrlLayout->addWidget(insertBtn); + ctrlLayout->addWidget(deleteBtn); + ctrlLayout->addWidget(upBtn); + ctrlLayout->addWidget(downBtn); + + QHBoxLayout* btnLayout = new QHBoxLayout; + btnLayout->setMargin(0); + btnLayout->setSpacing(6); + + topLayout->addLayout(ctrlLayout, 0, 0); + topLayout->addWidget(myDirList, 1, 0); + topLayout->addLayout(btnLayout, 2, 0); + + connect(myDirList, SIGNAL(mouseButtonClicked(int, QListBoxItem*, const QPoint&)), + this, SLOT(onMouseButtonClicked(int, QListBoxItem*, const QPoint&))); + connect(myDirList, SIGNAL(doubleClicked(QListBoxItem*)), + this, SLOT(onDblClicked(QListBoxItem*))); + + connect(insertBtn, SIGNAL(clicked()), this, SLOT(onInsert())); + connect(deleteBtn, SIGNAL(clicked()), this, SLOT(onDelete())); + connect(upBtn, SIGNAL(clicked()), this, SLOT(onUp())); + connect(downBtn, SIGNAL(clicked()), this, SLOT(onDown())); +} + +/*! + Destructor +*/ +QtxDirListEditor::~QtxDirListEditor() { +} + +/*! + Gets list of paths +*/ +void QtxDirListEditor::getPathList(QStringList& list) { + list.clear(); + for (unsigned i = 0; i < myDirList->count()-1; i++) + list.append(myDirList->text(i)); +} + +/*! + Sets list of paths +*/ +void QtxDirListEditor::setPathList(const QStringList& list) { + myDirList->clear(); + myDirList->insertItem(tr("")); + for (unsigned i = 0; i < list.count(); i++) + myDirList->insertItem(list[i], myDirList->count()-1); +} + +/*! + Validates entered path, returns true if OK +*/ +#ifndef WNT +#include +#endif +bool QtxDirListEditor::validate() { + if (myEdited) { + QString dirPath = myEdit->text().stripWhiteSpace(); +#ifndef WNT + if ( dirPath.startsWith( "~") ) { + dirPath = dirPath.remove(0,1); + QString user; + int slashPos = dirPath.find("/"); + if ( slashPos >= 0 ) { + user = dirPath.left(slashPos); + dirPath = dirPath.mid(slashPos); + } + else { + user = dirPath; + dirPath = ""; + } + if ( user.isEmpty() ) + user = getenv( "USER" ); + + struct passwd* user_data = getpwnam( user.latin1() ); + if ( user_data == NULL ) { + // unknown user or something another error + QMessageBox::critical(this, + tr("ERR_ERROR"), + tr("Unknown user %1").arg(user), + tr("BUT_OK")); + myEdit->setFocus(); + return false; + } + dirPath = user_data->pw_dir + dirPath; + } +#endif + QDir dir(dirPath); + QListBoxItem* found = 0; + for (unsigned i = 0; i < myDirList->count()-1; i++) { + QDir aDir(myDirList->text(i)); + if ( aDir.canonicalPath().isNull() && myDirList->text(i) == dir.absPath() || + !aDir.canonicalPath().isNull() && aDir.exists() && aDir.canonicalPath() == dir.canonicalPath()) { + found = myDirList->item(i); + break; + } + } + if (dirPath.isEmpty()) { + if (found) { + // it should be last (empty) item in the list - nothing to do + return true; + } + else { + // delete directory from the list + removeDir(myLastSelected); + return true; + } + } + else { + if (found) { + if (found != myLastSelected) { + // it is forbidden to add directory more then once + QMessageBox::critical(this, + tr("ERR_ERROR"), + tr("Directory already specified."), + tr("BUT_OK")); + myEdit->setFocus(); + return false; + } + } + else { + if (!dir.exists()) { + if ( QMessageBox::information(this, + tr("WRN_WARNING"), + tr("%1\n\nThe directory doesn't exist.\nAdd directory anyway?").arg(dir.absPath()), + tr("BUT_YES"), tr("BUT_NO"), QString::null, 1, 1) == 1) { + myEdit->setFocus(); + return false; + } + } + // append + appendDir(myLastSelected, dir.absPath()); + } + } + } + return true; +} + +/*! + Appends/changes directory +*/ +void QtxDirListEditor::appendDir(QListBoxItem* item, const QString& dir) { + int index = myDirList->index(item); + if (index >= 0 && index < (int)myDirList->count()) { + if (index == (int)myDirList->count()-1) { + // it is the last item (new), well, insert it before the last (empty) + myDirList->insertItem(dir, myDirList->count()-1); + } + else { + // change item + myDirList->changeItem(dir, index); + } + } +} + +/*! + Removes directory from list +*/ +void QtxDirListEditor::removeDir(QListBoxItem* item) { + // do not remove last item (empty) + int index = myDirList->index(item); + if (index >= 0 && index < (int)myDirList->count()-1) { + delete item; + myLastSelected = myDirList->item(index); + myDirList->setSelected(myLastSelected, true); + } +} + +/*! + Resize event +*/ +void QtxDirListEditor::resizeEvent(QResizeEvent* event) { + QWidget::resizeEvent(event); + if ( myEdited ) { + myEdit->resize(myDirList->viewport()->width()-myBtn->sizeHint().width(), myEdit->height()); + myBtn->move(myEdit->width(), myEdit->y()); + } +} + +/*! + Called when user clicks inside directories list box +*/ +void QtxDirListEditor::onMouseButtonClicked(int button, + QListBoxItem* item, + const QPoint& point) { + if (myEdited) { + if (!validate()) { + myDirList->setCurrentItem(myLastSelected); + myDirList->setSelected(myLastSelected, true); + return; + } + delete myEdit; + delete myBtn; + myEdit = 0; + myBtn = 0; + myEdited = false; + myDirList->setFocus(); + } + if (item) { + myDirList->setCurrentItem(item); + myDirList->setSelected(item, true); + myDirList->ensureCurrentVisible(); + qApp->processEvents(); + if (button == LeftButton && myLastSelected == item) { + QRect ir = myDirList->itemRect(myLastSelected); + + myEdit = new QLineEdit(myDirList->viewport()); + myBtn = new QToolButton(myDirList->viewport()); + myBtn->setText(" ... "); + connect(myBtn, SIGNAL(clicked()), this, SLOT(onBtnClicked())); + myEdit->setGeometry(0, + ir.top()-(myEdit->sizeHint().height()-ir.height())/2, + myDirList->viewport()->width()-myBtn->sizeHint().width(), + myEdit->sizeHint().height()); + myBtn->setGeometry (myEdit->width(), + ir.top()-(myEdit->sizeHint().height()-ir.height())/2, + myBtn->sizeHint().width(), + myEdit->sizeHint().height()); + connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditFinished())); + myEdited = true; + myEdit->show(); + myBtn->show(); + if (myDirList->index(myLastSelected) != (int)myDirList->count()-1) + myEdit->setText(myLastSelected->text()); + myEdit->selectAll(); + myEdit->setCursorPosition(myEdit->text().length()); + myEdit->installEventFilter(this); + myEdit->setFocus(); + } + } + else { + myDirList->clearSelection(); + } + myLastSelected = item; +} + +/*! + Called when user double-clicks on any item +*/ +void QtxDirListEditor::onDblClicked(QListBoxItem* item) { + onMouseButtonClicked(LeftButton, item, QPoint(0,0)); +} + +/*! + <...> (Browse dir) button slot +*/ +void QtxDirListEditor::onBtnClicked() { + QString dir = myEdit->text().stripWhiteSpace().isEmpty() ? + QString::null : + myEdit->text().stripWhiteSpace(); + + dir = QFileDialog::getExistingDirectory(dir, this, 0, tr("Select directory"), true); + + if (!dir.isEmpty()) { + myEdit->setText(dir); + myEdit->selectAll(); + myEdit->setCursorPosition(myEdit->text().length()); + } +} + +/*! + Called when user finises editing of path by pressing +*/ +void QtxDirListEditor::onEditFinished() { + if (myEdit) { + if (!validate()) { + myDirList->setCurrentItem(myLastSelected); + myDirList->setSelected(myLastSelected, true); + return; + } + delete myEdit; + delete myBtn; + myEdit = 0; + myBtn = 0; + myEdited = false; + myDirList->setFocus(); + } +} + +/*! + Event filter +*/ +bool QtxDirListEditor::eventFilter(QObject* object, QEvent* event) { + if ( myEdited ) { + if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) { + if (object == myDirList->horizontalScrollBar() || object == myDirList->verticalScrollBar()) { + if (!validate()) { + myDirList->setCurrentItem(myLastSelected); + myDirList->setSelected(myLastSelected, true); + return true; + } + delete myEdit; + delete myBtn; + myEdit = 0; + myBtn = 0; + myEdited = false; + myDirList->setFocus(); + } + } + else if (event->type() == QEvent::KeyPress) { + QKeyEvent* ke = (QKeyEvent*)event; + if (ke->key() == Key_Tab) + return true; + if (object == myDirList) { + return true; + } + else if (object == myEdit) { + if ( ke->key() == Key_Up || ke->key() == Key_Down || ke->key() == Key_PageUp || ke->key() == Key_PageDown || + ( ke->key() == Key_Home || ke->key() == Key_End || ke->key() == Key_Prior || ke->key() == Key_Next ) && + (ke->state() & ControlButton) ) { + return true; + } + else if ( ke->key() == Key_Escape ) { + delete myEdit; + delete myBtn; + myEdit = 0; + myBtn = 0; + myEdited = false; + myDirList->setFocus(); + return true; + } + } + } + } + return QWidget::eventFilter(object, event); +} + +/*! + button slot +*/ +void QtxDirListEditor::onInsert() { + if (!myEdited) { + myLastSelected = 0; + onMouseButtonClicked(LeftButton, myDirList->item(myDirList->count()-1), QPoint(0,0)); + onMouseButtonClicked(LeftButton, myDirList->item(myDirList->count()-1), QPoint(0,0)); + } +} + +/*! + button slot +*/ +void QtxDirListEditor::onDelete() { + if (!myEdited && myDirList->currentItem() >=0) { + removeDir(myDirList->item(myDirList->currentItem())); + myDirList->setFocus(); + } +} + +/*! + button slot +*/ +void QtxDirListEditor::onUp() { + if (!myEdited && myLastSelected) { + int index = myDirList->currentItem(); + if (index > 0 && index < (int)myDirList->count()-1 && myDirList->isSelected(index)) { + QString t = myDirList->text(index-1); + myDirList->changeItem(myDirList->text(index), index-1); + myDirList->changeItem(t, index); + myDirList->setCurrentItem(index-1); + myLastSelected = myDirList->item(index-1); + myDirList->setSelected(myLastSelected, true); + myDirList->setFocus(); + } + } +} + +/*! + button slot +*/ +void QtxDirListEditor::onDown() { + if (!myEdited && myLastSelected) { + int index = myDirList->currentItem(); + if (index >= 0 && index < (int)myDirList->count()-2 && myDirList->isSelected(index)) { + QString t = myDirList->text(index+1); + myDirList->changeItem(myDirList->text(index), index+1); + myDirList->changeItem(t, index); + myDirList->setCurrentItem(index+1); + myLastSelected = myDirList->item(index+1); + myDirList->setSelected(myLastSelected, true); + myDirList->setFocus(); + } + } +} diff --git a/src/Qtx/QtxDirListEditor.h b/src/Qtx/QtxDirListEditor.h new file mode 100644 index 000000000..f16343391 --- /dev/null +++ b/src/Qtx/QtxDirListEditor.h @@ -0,0 +1,131 @@ +#ifndef QTX_DIRLISTEDITOR_H +#define QTX_DIRLISTEDITOR_H + +#include "Qtx.h" + +#include +#include +#include +#include +#include + +#ifdef WIN32 +#pragma warning( disable:4251 ) +#endif + +/*! + * \brief The GUI implementation of the directory list + */ +class QTX_EXPORT QtxDirListEditor : public QWidget { + + Q_OBJECT + +public: + + /*! + * \brief Constructor + * \param parent - the parent of the widget + */ + QtxDirListEditor(QWidget* parent); + + /*! + * \brief Destructor + */ + ~QtxDirListEditor(); + + /*! + * \brief Gets list of paths + * \param list - the returned reference to the list of paths + */ + void getPathList(QStringList& list); + + /*! + * \brief Sets list of paths + * \param list - the list of paths to set + */ + void setPathList(const QStringList& list); + + /*! + * \brief Event filter, redefined from QObject class + */ + bool eventFilter(QObject* object, QEvent* event); + +protected: + + /*! + * \brief Validates entered path + * \retval bool - returns status (true if OK) + */ + bool validate(); + + /*! + * \brief Appends/changes path + * \param item - the item in QListBox + * \param dir - the path + */ + void appendDir(QListBoxItem* item, const QString& dir); + + /*! + * \brief Removes directory from list + * \param item - the item in QListBox + */ + void removeDir(QListBoxItem* item); + + /*! + * \brief Resize event handler, reimplemented from QWidget + * \param event - the resize event + */ + void resizeEvent(QResizeEvent* event); + +protected slots: + + /*! + * \brief Called when user clicks inside directories list box + */ + void onMouseButtonClicked(int, QListBoxItem*, const QPoint&); + + /*! + * \brief Called when user double-clicks on any item + */ + void onDblClicked(QListBoxItem*); + + /*! + * \brief <...> (Browse dir) button slot + */ + void onBtnClicked(); + + /*! + * \brief Ccalled when user finises editing of path by pressing + */ + void onEditFinished(); + + /*! + * \brief button slot + */ + void onInsert(); + + /*! + * \brief button slot + */ + void onDelete(); + + /*! + * \brief button slot + */ + void onUp(); + + /*! + * \brief button slot + */ + void onDown(); + +private: + QListBox* myDirList; //!< directory list + QLineEdit* myEdit; //!< path edit box + QToolButton* myBtn; //!< browse pah button + bool myEdited; //!< edit mode flag + QListBoxItem* myLastSelected; //!< last selected row + +}; + +#endif diff --git a/src/Qtx/QtxListResourceEdit.cxx b/src/Qtx/QtxListResourceEdit.cxx index d2d99355a..ac06643df 100644 --- a/src/Qtx/QtxListResourceEdit.cxx +++ b/src/Qtx/QtxListResourceEdit.cxx @@ -22,6 +22,8 @@ #include "QtxIntSpinBox.h" #include "QtxDblSpinBox.h" +#include "QtxDirListEditor.h" + /* Class: QtxListResourceEdit Descr: GUI implementation of QtxResourceEdit - manager of resources @@ -454,6 +456,9 @@ QtxResourceEdit::Item* QtxListResourceEdit::Group::createItem( const QString& ti case Font: item = new FontItem( title, resourceEdit(), this, this ); break; + case DirList: + item = new DirListItem( title, resourceEdit(), this, this ); + break; } return item; @@ -971,3 +976,25 @@ void QtxListResourceEdit::FontItem::onSelectFont() buildFontPrs(); } } + +QtxListResourceEdit::DirListItem::DirListItem( const QString& title, QtxResourceEdit* edit, Item* pItem, QWidget* parent ) +: PrefItem( Font, edit, pItem, parent ) +{ + myDirListEditor = new QtxDirListEditor( this ); +} + +QtxListResourceEdit::DirListItem::~DirListItem() +{ +} + +void QtxListResourceEdit::DirListItem::store() +{ + QStringList list; + myDirListEditor->getPathList(list); + setString( QString(list.join(";")) ); +} + +void QtxListResourceEdit::DirListItem::retrieve() +{ + myDirListEditor->setPathList(QStringList::split(";", getString())); +} diff --git a/src/Qtx/QtxListResourceEdit.h b/src/Qtx/QtxListResourceEdit.h index 3dfe65f74..141d0aba0 100644 --- a/src/Qtx/QtxListResourceEdit.h +++ b/src/Qtx/QtxListResourceEdit.h @@ -22,6 +22,8 @@ class QWidgetStack; class QtxIntSpinBox; class QtxDblSpinBox; +class QtxDirListEditor; + /* Class: QtxListResourceEdit Descr: GUI implementation of QtxResourceEdit - manager of resources @@ -47,8 +49,9 @@ public: class IntegerSpinItem; class IntegerEditItem; class FontItem; + class DirListItem; - enum { Space, Bool, Color, String, Selector, DblSpin, IntSpin, Double, Integer, GroupBox, Font, User }; + enum { Space, Bool, Color, String, Selector, DblSpin, IntSpin, Double, Integer, GroupBox, Font, DirList, User }; public: QtxListResourceEdit( QtxResourceMgr*, QWidget* = 0 ); @@ -384,4 +387,39 @@ private: QLabel* myFontPrs; }; + +/*! + * \brief GUI implementation of resources directory list item. + * + * + */ +class QtxListResourceEdit::DirListItem : public PrefItem +{ + Q_OBJECT + +public: + + /*! + * \brief Constructor + */ + DirListItem( const QString&, QtxResourceEdit*, Item*, QWidget* = 0 ); + /*! + * \brief Destructor + */ + virtual ~DirListItem(); + + /*! + * \brief Stores the data + */ + virtual void store(); + + /*! + * \brief Retrieves the data + */ + virtual void retrieve(); + +private: + QtxDirListEditor* myDirListEditor; //!< The widget wich implements in GUI the list of directories +}; + #endif diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index df8311669..126d2bdeb 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -1344,6 +1344,12 @@ void SalomeApp_Application::createPreferences( SalomeApp_Preferences* pref ) pref->setItemProperty( vtkTS, "min", 1 ); pref->setItemProperty( vtkTS, "max", 150 ); + + int dirTab = pref->addPreference( tr( "PREF_TAB_DIRECTORIES" ), salomeCat ); + int dirGroup = pref->addPreference( tr( "PREF_GROUP_DIRECTORIES" ), dirTab ); + pref->setItemProperty( dirGroup, "columns", 1 ); + pref->addPreference( tr( "" ), dirGroup, + SalomeApp_Preferences::DirList, "FileDlg", "QuickDirList" ); } void SalomeApp_Application::preferencesChanged( const QString& sec, const QString& param ) diff --git a/src/SalomeApp/resources/SalomeApp_msg_en.po b/src/SalomeApp/resources/SalomeApp_msg_en.po index 17aee442a..0582fcc63 100644 --- a/src/SalomeApp/resources/SalomeApp_msg_en.po +++ b/src/SalomeApp/resources/SalomeApp_msg_en.po @@ -177,6 +177,12 @@ msgstr "Number of isolines along V" msgid "SalomeApp_Application::PREF_TRIHEDRON_SHOW" msgstr "Show trihedron" +msgid "SalomeApp_Application::PREF_TAB_DIRECTORIES" +msgstr "Directories" + +msgid "SalomeApp_Application::PREF_GROUP_DIRECTORIES" +msgstr "Quick directory list" + //======================================================================================= msgid "SalomeApp_Application::OBJ_BROWSER_NAME"