1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <DFBrowser_SearchLine.hxx>
17 #include <DFBrowser_SearchLineModel.hxx>
18 #include <DFBrowser_Window.hxx>
20 #include <DFBrowserPane_Tools.hxx>
21 #include <DFBrowser_Module.hxx>
23 #include <TDocStd_Application.hxx>
24 #include <TDocStd_Document.hxx>
25 #include <TDF_AttributeIterator.hxx>
26 #include <TDF_ChildIterator.hxx>
28 #include <QStringList>
30 #include <QHBoxLayout>
32 #include <QToolButton>
35 #include <QPaintEvent>
39 //! class DFBrowser_LineEdit
40 //! Extension of Qt line edit to visualize help text until the line edit control has not been filled yet
41 class DFBrowser_LineEdit : public QLineEdit
46 DFBrowser_LineEdit (QWidget* theParent) : QLineEdit(theParent) {}
49 virtual ~DFBrowser_LineEdit() {}
51 //! Sets text that is shown in line edit it the text of this control is empty
52 //! \param theText a string value
53 void setPlaneHolder (const QString& theText) { myPlaceHolder = theText; }
55 //! Draws the line edit context, put plane holder if text is empty
56 //! \param theEvent a paint event
57 virtual void paintEvent (QPaintEvent* theEvent) Standard_OVERRIDE
59 QLineEdit::paintEvent (theEvent);
60 if (!text().isEmpty())
63 QPainter aPainter (this);
64 QFontMetrics aFontMetrics = fontMetrics();
65 QRect aLineRect = rect();
66 Qt::Alignment anAlignment = QStyle::visualAlignment (layoutDirection(), Qt::AlignLeft);
68 QColor aColor = palette().text().color();
69 aColor.setAlpha (128);
70 QPen anOldpen = aPainter.pen();
71 aPainter.setPen (aColor);
72 aLineRect.adjust (4, 4, 0, 0);
73 QString anElidedText = aFontMetrics.elidedText (myPlaceHolder, Qt::ElideRight, aLineRect.width());
74 aPainter.drawText (aLineRect, anAlignment, anElidedText);
75 aPainter.setPen (anOldpen);
80 QString myPlaceHolder; //!< text of filling line edit content if the text is empty
83 // =======================================================================
84 // function : Constructor
86 // =======================================================================
87 DFBrowser_SearchLine::DFBrowser_SearchLine (QWidget* theParent)
90 QHBoxLayout* aLayout = new QHBoxLayout (this);
91 aLayout->setContentsMargins (0, 0, 0, 0);
92 aLayout->setSpacing (0);
94 myLineControl = new DFBrowser_LineEdit (this);
95 ((DFBrowser_LineEdit*)myLineControl)->setPlaneHolder (QString (tr ("Scanning application ...")));
96 mySearchButton = new QToolButton (this);
97 mySearchButton->setIcon (QIcon (":/icons/search.png"));
99 QCompleter* aCompleter = new QCompleter (this);
100 aCompleter->setCaseSensitivity (Qt::CaseInsensitive);
101 myLineControl->setCompleter (aCompleter);
103 aLayout->addWidget (myLineControl);
104 aLayout->addWidget (mySearchButton);
106 connect (myLineControl, SIGNAL (textChanged (const QString&)), this, SLOT (onTextChanged (const QString&)));
107 connect (myLineControl, SIGNAL (returnPressed()), this, SLOT (onReturnPressed()));
108 connect (mySearchButton, SIGNAL (clicked()), this, SLOT (onSearchButtonClicked()));
110 DFBrowser_Window::SetWhiteBackground (this);
113 // =======================================================================
114 // function : SetModule
116 // =======================================================================
117 void DFBrowser_SearchLine::SetModule (DFBrowser_Module* theModule)
119 DFBrowser_SearchLineModel* aModel = new DFBrowser_SearchLineModel (myLineControl, theModule);
120 myLineControl->completer()->setModel (aModel);
123 // =======================================================================
124 // function : GetModule
126 // =======================================================================
127 DFBrowser_Module* DFBrowser_SearchLine::GetModule()
129 DFBrowser_SearchLineModel* aModel = dynamic_cast<DFBrowser_SearchLineModel*> (GetModel());
130 return aModel->GetModule();
133 // =======================================================================
134 // function : SetValues
136 // =======================================================================
137 void DFBrowser_SearchLine::SetValues (const QMap<int, QMap<QString, DFBrowser_SearchItemInfo > >& theDocumentValues,
138 const QMap<int, QStringList>& theDocumentInfoValues)
140 DFBrowser_SearchLineModel* aModel = dynamic_cast<DFBrowser_SearchLineModel*> (GetModel());
141 aModel->SetValues (theDocumentValues, theDocumentInfoValues);
143 QString aFirstValue = !theDocumentInfoValues.empty() ? theDocumentInfoValues.begin().value().first() : "";
144 DFBrowser_LineEdit* aLineEdit = dynamic_cast<DFBrowser_LineEdit*> (myLineControl);
145 aLineEdit->setPlaneHolder (QString (tr ("Search : %1")).arg (aFirstValue));
148 // =======================================================================
149 // function : ClearValues
151 // =======================================================================
152 void DFBrowser_SearchLine::ClearValues()
154 DFBrowser_SearchLineModel* aModel = dynamic_cast<DFBrowser_SearchLineModel*> (GetModel());
155 aModel->ClearValues();
157 DFBrowser_LineEdit* aLineEdit = dynamic_cast<DFBrowser_LineEdit*> (myLineControl);
158 aLineEdit->setPlaneHolder(QString (tr ("Scanning application ...")));
161 // =======================================================================
162 // function : onTextChanged
164 // =======================================================================
165 void DFBrowser_SearchLine::onTextChanged (const QString& theText)
167 mySearchButton->setIcon (theText.isEmpty() ? QIcon (":/icons/search.png")
168 : QIcon (":/icons/search_cancel.png"));
169 emit searchActivated();
172 // =======================================================================
173 // function : onSearchButtonClicked
175 // =======================================================================
176 void DFBrowser_SearchLine::onSearchButtonClicked()
178 if (!Text().isEmpty())