From: vsv Date: Wed, 28 Oct 2015 15:16:18 +0000 (+0300) Subject: Make label on ObjectBrowser top similar to tree node X-Git-Tag: V_2.0.0_alfa1~64 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f0f116c06d59831ad40a594c022686449a3e17f9;p=modules%2Fshaper.git Make label on ObjectBrowser top similar to tree node --- diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index a083bc3db..56b242561 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -14,7 +14,6 @@ #include #include -#include #include #include #include @@ -170,6 +169,79 @@ void XGUI_DataTree::onDoubleClick(const QModelIndex& theIndex) } } +//******************************************************************** +//******************************************************************** +//******************************************************************** +XGUI_ActiveDocLbl::XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent ) + : QLineEdit(theText, theParent), + myPreSelectionStyle(""), + myNeutralStyle(""), + mySelectionStyle(""), + myIsSelected(false) +{ +} + +void XGUI_ActiveDocLbl::setTreeView(QTreeView* theView) +{ + myTreeView = theView; + QPalette aPalet = myTreeView->palette(); + QColor aHighlight = aPalet.highlight().color(); + + myPreSelectionStyle = "QLineEdit {background-color: "; + myPreSelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 white, stop:1 " + aHighlight.lighter(170).name() + ");"; + myPreSelectionStyle += "border: 1px solid lightblue; border-radius: 2px }"; + + QString aName = aPalet.color(QPalette::Base).name(); + myNeutralStyle = "QLineEdit { border: 1px solid " + aName + " }"; + + mySelectionStyle = "QLineEdit {background-color: "; + mySelectionStyle += "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 " + aHighlight.lighter(170).name(); + mySelectionStyle += ", stop:1 " + aHighlight.lighter().name() + ");"; + mySelectionStyle += "border: 1px solid lightblue; border-radius: 2px }"; + + connect(myTreeView->selectionModel(), + SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), + SLOT(unselect())); +} + + +bool XGUI_ActiveDocLbl::event(QEvent* theEvent) +{ + switch (theEvent->type()) { + case QEvent::Enter: + if (!myIsSelected) + setStyleSheet(myPreSelectionStyle); + break; + case QEvent::Leave: + if (!myIsSelected) + setStyleSheet(myNeutralStyle); + break; + } + return QLineEdit::event(theEvent); +} + +static bool MYClearing = false; +void XGUI_ActiveDocLbl::mouseReleaseEvent( QMouseEvent* e) +{ + MYClearing = true; + myIsSelected = true; + setStyleSheet(mySelectionStyle); + // We can not block signals because on + // clear selection the View state will not be updated + myTreeView->clearSelection(); + QLineEdit::mouseReleaseEvent(e); + MYClearing = false; +} + +void XGUI_ActiveDocLbl::unselect() +{ + if (!MYClearing) { + myIsSelected = false; + setStyleSheet(myNeutralStyle); + } +} + + //******************************************************************** //******************************************************************** //******************************************************************** @@ -180,12 +252,8 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) ModuleBase_Tools::zeroMargins(aLayout); aLayout->setSpacing(0); - QFrame* aLabelWgt = new QFrame(this); - //QWidget* aLabelWgt = new QWidget(this); + QWidget* aLabelWgt = new QWidget(this); aLabelWgt->setAutoFillBackground(true); - QPalette aPalet = aLabelWgt->palette(); - aPalet.setColor(QPalette::Window, Qt::white); - aLabelWgt->setPalette(aPalet); aLayout->addWidget(aLabelWgt); QHBoxLayout* aLabelLay = new QHBoxLayout(aLabelWgt); @@ -195,35 +263,40 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) QLabel* aLbl = new QLabel(aLabelWgt); aLbl->setPixmap(QPixmap(":pictures/assembly.png")); aLbl->setMargin(2); - - aLbl->setAutoFillBackground(true); + // Do not paint background of the label (in order to show icon) + aLbl->setAutoFillBackground(false); aLabelLay->addWidget(aLbl); SessionPtr aMgr = ModelAPI_Session::get(); DocumentPtr aDoc = aMgr->moduleDocument(); - // TODO: Find a name of the root document - myActiveDocLbl = new QLineEdit(tr("Part set"), aLabelWgt); + myActiveDocLbl = new XGUI_ActiveDocLbl(tr("Part set"), aLabelWgt); myActiveDocLbl->setReadOnly(true); myActiveDocLbl->setFrame(false); - //myActiveDocLbl->setMargin(2); myActiveDocLbl->setContextMenuPolicy(Qt::CustomContextMenu); - myActiveDocLbl->installEventFilter(this); - aLabelLay->addWidget(myActiveDocLbl); aLabelLay->setStretch(1, 1); myTreeView = new XGUI_DataTree(this); - //myTreeView->setFrameShape(QFrame::NoFrame); + myTreeView->setFrameShape(QFrame::NoFrame); aLayout->addWidget(myTreeView); - aLabelWgt->setFrameShape(myTreeView->frameShape()); - aLabelWgt->setFrameShadow(myTreeView->frameShadow()); + QPalette aTreePalet = myTreeView->palette(); + QColor aTreeBack = aTreePalet.color(QPalette::Base); + + QPalette aPalet; + aPalet.setColor(QPalette::Base, aTreeBack); + aPalet.setColor(QPalette::Window, aTreeBack); + aLabelWgt->setPalette(aPalet); myDocModel = new XGUI_DataModel(this); myTreeView->setModel(myDocModel); + + // It has to be done after setting of model + myActiveDocLbl->setTreeView(myTreeView); + QItemSelectionModel* aSelMod = myTreeView->selectionModel(); connect(aSelMod, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&))); @@ -239,55 +312,6 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser() { } -//*************************************************** -bool XGUI_ObjectsBrowser::eventFilter(QObject* obj, QEvent* theEvent) -{ - if (obj == myActiveDocLbl) { - if (!myActiveDocLbl->isReadOnly()) { - // End of editing by mouse click - if (theEvent->type() == QEvent::MouseButtonRelease) { - QMouseEvent* aEvent = (QMouseEvent*) theEvent; - QPoint aPnt = mapFromGlobal(aEvent->globalPos()); - if (childAt(aPnt) != myActiveDocLbl) { - closeDocNameEditing(true); - } - } else if (theEvent->type() == QEvent::KeyRelease) { - QKeyEvent* aEvent = (QKeyEvent*) theEvent; - switch (aEvent->key()) { - case Qt::Key_Return: - case Qt::Key_Enter: // Accept current input - closeDocNameEditing(true); - break; - case Qt::Key_Escape: // Cancel the input - closeDocNameEditing(false); - break; - } - } - } else { - if (theEvent->type() == QEvent::MouseButtonDblClick) { - emit headerMouseDblClicked(QModelIndex()); - return true; - } - } - } - return QWidget::eventFilter(obj, theEvent); -} - -//*************************************************** -void XGUI_ObjectsBrowser::closeDocNameEditing(bool toSave) -{ - myActiveDocLbl->deselect(); - myActiveDocLbl->clearFocus(); - myActiveDocLbl->releaseMouse(); - myActiveDocLbl->setReadOnly(true); - if (toSave) { - // TODO: Save the name of root document - SessionPtr aMgr = ModelAPI_Session::get(); - DocumentPtr aDoc = aMgr->moduleDocument(); - } else { - myActiveDocLbl->setText(myActiveDocLbl->property("OldText").toString()); - } -} //*************************************************** void XGUI_ObjectsBrowser::onContextMenuRequested(QContextMenuEvent* theEvent) @@ -351,12 +375,6 @@ void XGUI_ObjectsBrowser::onEditItem() return; } } - //Selection happens in Upper label - myActiveDocLbl->setReadOnly(false); - myActiveDocLbl->setFocus(); - myActiveDocLbl->selectAll(); - myActiveDocLbl->grabMouse(); - myActiveDocLbl->setProperty("OldText", myActiveDocLbl->text()); } //*************************************************** diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index 53e9a5fb5..48f97e2e4 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -11,11 +11,44 @@ #include #include +#include class ModuleBase_IDocumentDataModel; -class QLineEdit; class XGUI_DataModel; +/** +* \ingroup GUI +* Implementation of root label in Object Browser +*/ +class XGUI_ActiveDocLbl: public QLineEdit +{ +Q_OBJECT + public: + /// Constructor + /// \param theParent a parent widget + XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent ); + + void setTreeView(QTreeView* theView); + + QTreeView* treePalette() const { return myTreeView;} + + virtual bool event(QEvent* theEvent); + +public slots: + void unselect(); + +protected: + virtual void mouseReleaseEvent( QMouseEvent* e); + +private: + QString myPreSelectionStyle; + QString myNeutralStyle; + QString mySelectionStyle; + + QTreeView* myTreeView; + bool myIsSelected; +}; + /** * \ingroup GUI * Implementation of Data Tree object for Object Browser @@ -119,10 +152,6 @@ signals: //! Segnal is emitted when user cliks by mouse in header label of object browser void headerMouseDblClicked(const QModelIndex&); - protected: - /// Redefinition of virtual method - virtual bool eventFilter(QObject* obj, QEvent* theEvent); - private slots: /// Show context menu /// \param theEvent a context menu event @@ -136,11 +165,10 @@ signals: void onSelectionChanged(const QItemSelection& theSelected, const QItemSelection& theDeselected); private: - void closeDocNameEditing(bool toSave); //! Internal model XGUI_DataModel* myDocModel; - QLineEdit* myActiveDocLbl; + XGUI_ActiveDocLbl* myActiveDocLbl; XGUI_DataTree* myTreeView; };