3 // Copyright (C) 2005 CEA/DEN, EDF R&D
7 // File : SalomeApp_ListView.cxx
8 // Author : Vadim SANDLER
12 #include "SalomeApp_ListView.h"
13 #include "SalomeApp_Application.h"
15 #include "SUIT_ResourceMgr.h"
16 #include "SUIT_Session.h"
19 #include <qvalidator.h>
20 #include <qapplication.h>
21 #include <qtoolbutton.h>
23 #include <TColStd_ListIteratorOfListOfInteger.hxx>
24 #include <TColStd_ListIteratorOfListOfReal.hxx>
26 #include "utilities.h"
30 //////////////////////////////////////////////////////////////////////
31 // SalomeApp_ListView class implementation
32 //////////////////////////////////////////////////////////////////////
34 //================================================================
35 // Function : computeEditGeometry
36 /*! Purpose : static function - used for resizing editing widget*/
37 //================================================================
38 void computeEditGeometry(SalomeApp_ListViewItem* theItem,
39 SalomeApp_EntityEdit* theWidget)
43 QListView* aListView = theItem->listView();
44 int anEditColumn = theItem->getEditedColumn();
48 int aX = 0, aY = 0, aW = 0, aH = 0;
50 QRect aRect = aListView->itemRect(theItem);
51 aListView->contentsToViewport(aListView->header()->sectionPos(anEditColumn), 0, aX, aY);
53 aX = 0; // THIS CAN BE REMOVED
54 QSize aSize = theWidget->getControl()->sizeHint();
55 aH = QMAX(aSize.height() , aRect.height() );
56 aY = aRect.y() - ((aH - aRect.height()) / 2);
57 //aW = aListView->columnWidth(anEditColumn); // CAN SUBSTITUTE NEXT 3 ROWS
58 aW = aListView->viewport()->width() - aX;
61 theWidget->setGeometry(aX, aY, aW, aH);
64 //================================================================
65 // Function : SalomeApp_ListView::SalomeApp_ListView
66 /*! Purpose : constructor*/
67 //================================================================
68 SalomeApp_ListView::SalomeApp_ListView( QWidget* parent )
69 : QtxListView( parent )
71 myMouseEnabled = true;
72 myEditingEnabled = false;
73 setSelectionMode(Single);
75 setRootIsDecorated(false);
76 setAllColumnsShowFocus(false);
77 // header()->setClickEnabled(false);
78 header()->setMovingEnabled(false);
83 viewport()->installEventFilter(this);
85 connect(this, SIGNAL(selectionChanged()),
86 this, SLOT(onSelectionChanged()));
87 connect(header(), SIGNAL(sizeChange(int, int, int)),
88 this, SLOT(onHeaderSizeChange(int, int, int)));
91 //================================================================
92 // Function : SalomeApp_ListView::~SalomeApp_ListView
93 /*! Purpose : destructor*/
94 //================================================================
95 SalomeApp_ListView::~SalomeApp_ListView()
104 //================================================================
105 // Function : SalomeApp_ListView::updateViewer
106 /*! Purpose : updates all data viewer*/
107 //================================================================
108 void SalomeApp_ListView::updateViewer()
110 // temporary disconnecting selection changed SIGNAL
112 SalomeApp_ListViewItem* aRoot = (SalomeApp_ListViewItem*)firstChild();
114 aRoot->updateAllLevels();
116 // connecting again selection changed SIGNAL
118 emit selectionChanged();
121 //================================================================
122 // Function : SalomeApp_ListView::updateSelected
123 /*! Purpose : updates currently selected item(s)*/
124 //================================================================
125 void SalomeApp_ListView::updateSelected()
127 // temporary disconnecting selection changed SIGNAL
129 SalomeApp_ListViewItem* aChild = (SalomeApp_ListViewItem*)selectedItem();
131 aChild->updateAllLevels();
133 // connecting again selection changed SIGNAL
135 emit selectionChanged();
138 //================================================================
139 // Function : SalomeApp_ListView::popupClientType
140 /*! Purpose : returns popup client type*/
141 //================================================================
142 QString SalomeApp_ListView::popupClientType() const
144 return "SalomeApp_ListView";
147 //================================================================
148 // Function : SalomeApp_ListView::contextMenuPopup
149 /*! Purpose : fills popup menu with items*/
150 //================================================================
151 void SalomeApp_ListView::contextMenuPopup( QPopupMenu* aPopup )
158 //================================================================
159 // Function : SalomeApp_ListView::clear
160 /*! Purpose : clears view*/
161 //================================================================
162 void SalomeApp_ListView::clear()
172 //================================================================
173 // Function : SalomeApp_ListView::isMouseEnabled
174 /*! Purpose : returms true if mouse events are enabled*/
175 //================================================================
176 bool SalomeApp_ListView::isMouseEnabled()
178 return myMouseEnabled;
181 //================================================================
182 // Function : SalomeApp_ListView::enableMouse
183 // Purpose : enabled/disables mouse events (excluding MouseMove)
184 //================================================================
185 void SalomeApp_ListView::enableMouse(bool enable)
187 myMouseEnabled = enable;
190 //================================================================
191 // Function : SalomeApp_ListView::eventFilter
192 /*! Purpose : event filter*/
193 //================================================================
194 bool SalomeApp_ListView::eventFilter(QObject* object, QEvent* event)
196 if (object == viewport() &&
197 (event->type() == QEvent::MouseButtonPress ||
198 event->type() == QEvent::MouseButtonRelease ||
199 event->type() == QEvent::MouseButtonDblClick) &&
203 return QListView::eventFilter(object, event);
206 //================================================================
207 // Function : SalomeApp_ListView::enableEditing
208 /*! Purpose : setting editing of items availbale/not available*/
209 //================================================================
210 void SalomeApp_ListView::enableEditing(bool theFlag)
212 myEditingEnabled = theFlag;
213 if (!myEditingEnabled) {
222 //================================================================
223 // Function : SalomeApp_ListView::isEnableEditing
224 /*! Purpose : says if editing is enabled*/
225 //================================================================
226 bool SalomeApp_ListView::isEnableEditing()
228 return myEditingEnabled;
231 //================================================================
232 // Function : SalomeApp_ListView::accept
233 /*! Purpose : calls finishEditing(true)...*/
234 //================================================================
235 void SalomeApp_ListView::accept()
240 //================================================================
241 // Function : QAD_ListView::onSelectionChanged
242 /*! Purpose : slot, called when selection changed in List Viewer*/
243 //================================================================
244 void SalomeApp_ListView::onSelectionChanged()
250 if (myEditedItem && !myEditedItem->isAccepted()) {
256 // editing is allowed in Single Selection Mode only
257 if (selectionMode() != Single || !isEnableEditing())
259 SalomeApp_ListViewItem* anItem = (SalomeApp_ListViewItem*)selectedItem();
261 if (!anItem->isEditable())
263 myEdit = anItem->startEditing();
265 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
266 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
267 myEditedItem = anItem;
274 //================================================================
275 // Function : SalomeApp_ListView::resizeEvent
276 /*! Purpose : called when Data Viewer is resized*/
277 //================================================================
278 void SalomeApp_ListView::resizeEvent( QResizeEvent * e)
280 QListView::resizeEvent(e);
281 int aW = columnWidth(columns()-1);
282 int aX = header()->sectionPos(columns()-1);
283 if (aW < width() - frameWidth() * 2 - aX - 1)
284 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
288 //================================================================
289 // Function : SalomeApp_ListView::onHeaderSizeChange
290 /*! Purpose : slot, called when columns sizes are changed*/
291 //================================================================
292 void SalomeApp_ListView::onHeaderSizeChange(int, int, int)
294 int aW = columnWidth(columns()-1);
295 int aX = header()->sectionPos(columns()-1);
296 if (aW < width() - frameWidth() * 2 - aX - 1)
297 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
300 //================================================================
301 // Function : SalomeApp_ListView::viewportPaintEvent
302 /*! Purpose : handler for paint event*/
303 //================================================================
304 void SalomeApp_ListView::viewportPaintEvent(QPaintEvent* e)
306 QListView::viewportPaintEvent(e);
307 if (myEditedItem && myEdit) {
308 computeEditGeometry(myEditedItem, myEdit);
312 //================================================================
313 // Function : SalomeApp_ListView::onEditOk
314 /*! Purpose : called when user finishes in editing of item*/
315 //================================================================
316 void SalomeApp_ListView::onEditOk()
321 //================================================================
322 // Function : SalomeApp_ListView::onEditCancel
323 /*! Purpose : called when user cancels item editing*/
324 //================================================================
325 void SalomeApp_ListView::onEditCancel()
327 finishEditing(false);
330 //================================================================
331 // Function : SalomeApp_ListView::finishEditing
332 /*! Purpose : finishes editing of entity*/
333 //================================================================
334 UpdateType SalomeApp_ListView::finishEditing(bool ok)
336 UpdateType aNeedsUpdate = utCancel;
337 if (myEditedItem && myEdit)
339 disconnect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
340 disconnect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
341 myEditedItem->setAccepted(true);
343 aNeedsUpdate = myEditedItem->finishEditing(myEdit);
344 if (aNeedsUpdate == utCancel) {
345 // something to do here on Cancel...
348 // something to do here on OK...
351 switch (aNeedsUpdate) {
355 myEditedItem->updateAllLevels();
361 SalomeApp_ListViewItem* aParent = (SalomeApp_ListViewItem*)(myEditedItem->parent());
363 aParent->updateAllLevels();
365 myEditedItem->updateAllLevels();
376 // doing the same as for utUpdateViewer here
377 // descendants can add extra processing
387 // hide <myEdit> widget
395 //================================================================
396 // Function : SalomeApp_ListView::tip
397 /*! Purpose : gets current tooltip for list view
398 * \retval valid rect in success
400 //================================================================
401 QRect SalomeApp_ListView::tip(QPoint aPos,
406 QRect result( -1, -1, -1, -1 );
407 SalomeApp_ListViewItem* aItem = (SalomeApp_ListViewItem*)itemAt( aPos );
409 for (int i = 0; i < columns(); i++) {
410 QRect aItemRect = aItem->itemRect(i);
411 QRect aTextRect = aItem->textRect(i);
412 if ( !aItem->text(i).isEmpty() &&
413 ( aItemRect.width() > header()->sectionSize(i) ||
414 aTextRect.left() < 0 ||
415 aTextRect.top() < 0 ||
416 aTextRect.right() > viewport()->width() ||
417 aTextRect.bottom() > viewport()->height() ) ) {
418 // calculating tip data
419 aText = aItem->tipText();
420 dspRect = aItem->tipRect();
422 if (dspRect.isValid()) {
423 result = QRect(QPoint(0, aItemRect.top()),
424 QSize(viewport()->width(), aItemRect.height()));
432 //////////////////////////////////////////////////////////////////////
433 // SalomeApp_ListViewItem Class Implementation
434 //////////////////////////////////////////////////////////////////////
436 //================================================================
437 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
438 /*! Purpose : constructor*/
439 //================================================================
440 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent) :
441 QListViewItem( parent )
446 //================================================================
447 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
448 /*! Purpose : constructor*/
449 //================================================================
450 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
451 SalomeApp_ListViewItem* after) :
452 QListViewItem( parent, after )
457 //================================================================
458 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
459 /*! Purpose : constructor*/
460 //================================================================
461 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
462 const QString& theName,
463 const bool theEditable) :
464 QListViewItem(parent, theName)
467 setEditable(theEditable);
470 //================================================================
471 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
472 /*! Purpose : constructor*/
473 //================================================================
474 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
475 const QString& theName,
476 const QString& theValue,
477 const bool theEditable) :
478 QListViewItem(parent, theName, theValue)
481 setEditable(theEditable);
484 //================================================================
485 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
486 /*! Purpose : constructor*/
487 //================================================================
488 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
489 const QString& theName,
490 const bool theEditable) :
491 QListViewItem(parent, theName)
494 setEditable(theEditable);
497 //================================================================
498 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
499 /*! Purpose : constructor*/
500 //================================================================
501 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
502 SalomeApp_ListViewItem* after,
503 const QString& theName,
504 const bool theEditable) :
505 QListViewItem(parent, after, theName)
508 setEditable(theEditable);
511 //================================================================
512 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
513 /*! Purpose : constructor*/
514 //================================================================
515 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
516 SalomeApp_ListViewItem* after,
517 const QString& theName,
518 const bool theEditable) :
519 QListViewItem(parent, after, theName)
522 setEditable(theEditable);
526 //================================================================
527 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
528 /*! Purpose : constructor*/
529 //================================================================
530 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
531 const QString& theName,
532 const QString& theValue,
533 const bool theEditable) :
534 QListViewItem(parent, theName, theValue)
537 setEditable(theEditable);
541 //================================================================
542 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
543 /*! Purpose : constructor*/
544 //================================================================
545 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
546 SalomeApp_ListViewItem* after,
547 const QString& theName,
548 const QString& theValue,
549 const bool theEditable) :
550 QListViewItem(parent, after, theName, theValue)
553 setEditable(theEditable);
556 //================================================================
557 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
558 /*! Purpose : constructor*/
559 //================================================================
560 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
561 SalomeApp_ListViewItem* after,
562 const QString& theName,
563 const QString& theValue,
564 const bool theEditable) :
565 QListViewItem(parent, after, theName, theValue)
568 setEditable(theEditable);
571 //================================================================
572 // Function : SalomeApp_ListViewItem::~SalomeApp_ListViewItem
573 /*! Purpose : destructor*/
574 //================================================================
575 SalomeApp_ListViewItem::~SalomeApp_ListViewItem()
579 //================================================================
580 // Function : SalomeApp_ListViewItem::init
581 /*! Purpose : initialization*/
582 //================================================================
583 void SalomeApp_ListViewItem::init()
587 myEditingType = (int)SalomeApp_EntityEdit::etLineEdit;
588 myValueType = (int)SalomeApp_EntityEdit::vtString;
593 //================================================================
594 // Function : SalomeApp_ListViewItem::getName
595 /*! Purpose : as default returns text in the first column*/
596 //================================================================
597 QString SalomeApp_ListViewItem::getName() const
599 return ( listView()->columns() > 0 ) ? text(0) : QString("");
602 //================================================================
603 // Function : SalomeApp_ListViewItem::setName
604 /*! Purpose : as default sets text in the first column*/
605 //================================================================
606 UpdateType SalomeApp_ListViewItem::setName(const QString& theName)
608 UpdateType aNeedsUpdate = utCancel;
609 if (listView()->columns() > 0) {
611 aNeedsUpdate = utNone;
616 //================================================================
617 // Function : SalomeApp_ListViewItem::getValue
618 /*! Purpose : as default returns text in the second column*/
619 //================================================================
620 QString SalomeApp_ListViewItem::getValue() const
622 return ( listView()->columns() > 1 ) ? text(1) : QString("");
625 //================================================================
626 // Function : SalomeApp_ListViewItem::setValue
627 /*! Purpose : as default sets text in the second column*/
628 //================================================================
629 UpdateType SalomeApp_ListViewItem::setValue(const QString& theValue)
631 UpdateType aNeedsUpdate = utCancel;
632 if (listView()->columns() > 1) {
633 setText(1, theValue);
634 aNeedsUpdate = utNone;
639 //================================================================
640 // Function : SalomeApp_ListViewItem::fullName
641 /*! Purpose : returns full path to the entity from the root*/
642 //================================================================
643 QString SalomeApp_ListViewItem::fullName()
645 QString aFullName = getName();
646 SalomeApp_ListViewItem* aParent = (SalomeApp_ListViewItem*)parent();
647 while(aParent != NULL) {
648 aFullName = aParent->getName() + QString(".") + aFullName;
649 aParent = (SalomeApp_ListViewItem*)(aParent->parent());
654 //================================================================
655 // Function : SalomeApp_ListViewItem::openAllLevels
656 /*! Purpose : expands all entities beginning from this level*/
657 //================================================================
658 void SalomeApp_ListViewItem::openAllLevels()
661 SalomeApp_ListViewItem* aChild = (SalomeApp_ListViewItem*)firstChild();
663 aChild->openAllLevels();
664 aChild = (SalomeApp_ListViewItem*)(aChild->nextSibling());
668 //================================================================
669 // Function : SalomeApp_ListViewItem::updateAllLevels
670 /*! Purpose : update all entites beginning from this level*/
671 //================================================================
672 void SalomeApp_ListViewItem::updateAllLevels()
674 SalomeApp_ListViewItem* aChild = (SalomeApp_ListViewItem*)firstChild();
676 aChild->updateAllLevels();
677 aChild = (SalomeApp_ListViewItem*)(aChild->nextSibling());
681 //================================================================
682 // Function : SalomeApp_EditBox::isEditable
683 /*! Purpose : return true if entity is editable*/
684 //================================================================
685 bool SalomeApp_ListViewItem::isEditable() const
690 //================================================================
691 // Function : SalomeApp_ListViewItem::setEditable
692 /*! Purpose : sets editable flag fo the entity*/
693 //================================================================
694 void SalomeApp_ListViewItem::setEditable(bool theEditable)
696 myEditable = theEditable;
699 //================================================================
700 // Function : SalomeApp_ListViewItem::isAccepted
701 /*! Purpose : returns true if entitiy is accepted after editing*/
702 //================================================================
703 bool SalomeApp_ListViewItem::isAccepted() const
708 //================================================================
709 // Function : SalomeApp_ListViewItem::setAccepted
710 /*! Purpose : set entitiy accepted or not after editing*/
711 //================================================================
712 void SalomeApp_ListViewItem::setAccepted(bool theAccepted)
714 myAccepted = theAccepted;
717 //================================================================
718 // Function : SalomeApp_ListViewItem::getEditingType
720 * \retval type of edit control (default is edit box)
723 * \li 2 - editable combo box
725 //================================================================
726 int SalomeApp_ListViewItem::getEditingType()
728 return myEditingType;
731 //================================================================
732 // Function : SalomeApp_ListViewItem::setEditingType
734 * \retval type of edit control (negative value means none)
737 * \li 2 - editable combo box
739 //================================================================
740 void SalomeApp_ListViewItem::setEditingType(const int type)
742 myEditingType = type;
745 //================================================================
746 // Function : SalomeApp_ListViewItem::getEditedColumn
748 /*! \retval edited column, default is last column
749 * negative value means there are no editable columns
751 //================================================================
752 int SalomeApp_ListViewItem::getEditedColumn()
754 return listView()->columns()-1;
757 //================================================================
758 // Function : SalomeApp_ListViewItem::getValueType
760 /*!\retval type of edited value (string, int, double)
763 //================================================================
764 int SalomeApp_ListViewItem::getValueType()
769 //================================================================
770 // Function : SalomeApp_ListViewItem::setValueType
771 /*! Purpose : sets type of edited value*/
772 //================================================================
773 void SalomeApp_ListViewItem::setValueType(const int valueType)
775 myValueType = valueType;
778 //================================================================
779 // Function : SalomeApp_ListViewItem::getUserType
780 /*! Purpose : sets type of edited value*/
781 //================================================================
782 int SalomeApp_ListViewItem::getUserType()
787 //================================================================
788 // Function : SalomeApp_ListViewItem::setUserType
789 /*! Purpose : sets type of edited value*/
790 //================================================================
791 void SalomeApp_ListViewItem::setUserType(const int userType)
793 myUserType = userType;
796 //================================================================
797 // Function : SalomeApp_ListViewItem::getButtons
798 /*! Purpose : returns buttons for editing widget (Apply (V), Cancel (X))
799 * default is both buttons
801 //================================================================
802 int SalomeApp_ListViewItem::getButtons()
807 //================================================================
808 // Function : SalomeApp_ListViewItem::getButtons
809 /*! Purpose : sets buttons for editing widget (Apply (V), Cancel (X))*/
810 //================================================================
811 void SalomeApp_ListViewItem::setButtons(const int buttons)
816 //================================================================
817 // Function : SalomeApp_ListViewItem::startEditing
818 /*! Purpose : creates control for editing and fills it with values*/
819 //================================================================
820 SalomeApp_EntityEdit* SalomeApp_ListViewItem::startEditing()
822 SalomeApp_EntityEdit* aWidget = 0;
823 QListView* aListView = listView();
827 int anEditType = getEditingType();
828 int aValueType = getValueType();
829 int aButtons = getButtons();
830 int anEditColumn = getEditedColumn();
831 if (anEditColumn < 0 || anEditType < 0)
833 aWidget = new SalomeApp_EntityEdit(aListView->viewport(),
836 aButtons & SalomeApp_EntityEdit::btApply,
837 aButtons & SalomeApp_EntityEdit::btCancel);
838 computeEditGeometry(this, aWidget);
840 fillWidgetWithValues(aWidget);
845 //================================================================
846 // Function : SalomeApp_ListViewItem::fillWidgetWithValues
847 /*! Purpose : fills widget with initial values (list or single value)*/
848 //================================================================
849 void SalomeApp_ListViewItem::fillWidgetWithValues(SalomeApp_EntityEdit* theWidget)
851 int anEditColumn = getEditedColumn();
852 if (theWidget && anEditColumn >= 0 && !text(anEditColumn).isEmpty())
853 theWidget->insertItem(text(anEditColumn), true);
856 //================================================================
857 // Function : SalomeApp_ListViewItem::finishEditing
858 /*! Purpose : finishes editing of entity*/
859 //================================================================
860 UpdateType SalomeApp_ListViewItem::finishEditing(SalomeApp_EntityEdit* theWidget)
862 UpdateType aNeedsUpdate = utCancel;
865 int anEditColumn = getEditedColumn();
866 switch (anEditColumn) {
868 aNeedsUpdate = setName(theWidget->getText());
871 aNeedsUpdate = setValue(theWidget->getText());
879 MESSAGE( "System error has been caught - SalomeApp_ListViewItem::finishEditing" )
884 //================================================================
885 // Function : SalomeApp_ListViewItem::tipRect
886 /*! Purpose : calculates rectangle which should contain item's tip*/
887 //================================================================
888 QRect SalomeApp_ListViewItem::tipRect()
890 QRect aRect = QRect(-1, -1, -1, -1);
891 QRect aItemRect = listView()->itemRect(this);
892 if ( !aItemRect.isValid() )
895 QString aTip = tipText();
896 if (!aTip.isEmpty()) {
897 QRect aRect0 = textRect(0);
898 QFont aFont(listView()->font());
899 QFontMetrics fm(aFont);
900 int iw = fm.width(aTip);
901 aRect = QRect(QPoint(aRect0.x() < 0 ? 0 : aRect0.x(),
909 //================================================================
910 // Function : SalomeApp_ListViewItem::tipText
911 /*! Purpose : returns text for tooltip*/
912 //================================================================
913 QString SalomeApp_ListViewItem::tipText()
915 QString aText = getName();
916 if (!getValue().isEmpty())
917 aText += QString(" : ") + getValue();
921 //================================================================
922 // Function : SalomeApp_ListViewItem::textRect
923 /*! Purpose : calculates rect of item text in viewport coordinates*/
924 //================================================================
925 QRect SalomeApp_ListViewItem::textRect(const int column) const
927 QRect aItemRect = listView()->itemRect( this );
928 if ( !aItemRect.isValid() )
931 QFont aFont(listView()->font());
932 QFontMetrics fm(aFont);
934 int decorWidth = ( listView()->rootIsDecorated() ) ?
935 ( listView()->treeStepSize() * (depth() + 1) ) :
936 ( listView()->treeStepSize() * depth() );
937 int pixmapWidth = ( pixmap(column) ) ?
938 pixmap(column)->width() + listView()->itemMargin() * 2 :
939 listView()->itemMargin();
941 for (int i = 0; i < column; i++)
942 prevWidth += listView()->header()->sectionSize(i);
945 ((column == 0) ? decorWidth : 0);
946 int iy = aItemRect.y();
947 int iw = fm.width(text(column));
948 int ih = aItemRect.height();
949 if (pixmap(column)) {
950 iy += listView()->itemMargin();
951 ih -= listView()->itemMargin() * 2;
953 ix -= listView()->contentsX();
955 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
959 //================================================================
960 // Function : SalomeApp_ListViewItem::itemRect
961 /*! Purpose : calculates rect of item data in viewport coordinates*/
962 //================================================================
963 QRect SalomeApp_ListViewItem::itemRect(const int column) const
965 QRect aItemRect = listView()->itemRect( this );
966 if ( !aItemRect.isValid() )
969 QFont aFont(listView()->font());
970 QFontMetrics fm(aFont);
972 int decorWidth = ( listView()->rootIsDecorated() ) ?
973 ( listView()->treeStepSize() * (depth() + 1) ) :
974 ( listView()->treeStepSize() * depth() );
975 int pixmapWidth = ( pixmap(column) ) ?
976 pixmap(column)->width() + listView()->itemMargin() * 2 :
979 for (int i = 0; i < column; i++)
980 prevWidth += listView()->header()->sectionSize(i);
982 int iy = aItemRect.y();
983 int iw = pixmapWidth +
984 listView()->itemMargin() * 2 +
985 ((column == 0) ? decorWidth : 0) +
986 fm.width(text(column));
987 int ih = aItemRect.height();
988 ix -= listView()->contentsX();
990 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
994 //////////////////////////////////////////////////////////////////////
995 // SalomeApp_EditBox class implementation
996 //////////////////////////////////////////////////////////////////////
998 //================================================================
999 // Function : SalomeApp_EditBox::SalomeApp_EditBox
1000 /*! Purpose : constructor*/
1001 //================================================================
1002 SalomeApp_EditBox::SalomeApp_EditBox(QWidget* parent) :
1007 //================================================================
1008 // Function : SalomeApp_EditBox::keyPressEvent
1009 /*! Purpose : event filter for key pressing*/
1010 //================================================================
1011 void SalomeApp_EditBox::keyPressEvent( QKeyEvent *e )
1013 if ( e->key() == Key_Escape )
1014 emit escapePressed();
1016 QLineEdit::keyPressEvent( e );
1020 //////////////////////////////////////////////////////////////////////
1021 // SalomeApp_ComboBox class implementation
1022 //////////////////////////////////////////////////////////////////////
1024 //================================================================
1025 // Function : SalomeApp_ComboBox::SalomeApp_ComboBox
1026 /*! Purpose : constructor*/
1027 //================================================================
1028 SalomeApp_ComboBox::SalomeApp_ComboBox(bool rw, QWidget* parent, const char* name) :
1029 QComboBox(rw, parent, name)
1033 //================================================================
1034 // Function : SalomeApp_ComboBox::findItem
1035 /*! Purpose : searches item in list and returns its index*/
1036 //================================================================
1037 int SalomeApp_ComboBox::findItem(const QString& theText)
1039 for (int i = 0; i < count(); i++)
1040 if (text(i) == theText)
1045 //================================================================
1046 // Function : SalomeApp_ComboBox::insertItem
1047 /*! Purpose : adds item in combo box*/
1048 //================================================================
1049 void SalomeApp_ComboBox::insertItem(const QString& theValue,
1052 if (duplicatesEnabled() || findItem(theValue) < 0)
1053 QComboBox::insertItem(theValue, theIndex);
1056 //================================================================
1057 // Function : SalomeApp_ComboBox::insertList
1058 /*! Purpose : adds list of items in combo box*/
1059 //================================================================
1060 void SalomeApp_ComboBox::insertList(const QStringList& theList)
1062 for (unsigned i = 0; i < theList.count(); i++)
1063 insertItem(theList[i]);
1066 //================================================================
1067 // Function : SalomeApp_ComboBox::insertItem
1068 /*! Purpose : adds item in combo box*/
1069 //================================================================
1070 void SalomeApp_ComboBox::insertItem(const int theValue)
1074 for (int i = 0; i < count(); i++) {
1075 aNum = text(i).toInt(&bOk);
1077 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1078 insertItem(QString::number(theValue), i);
1083 insertItem(QString::number(theValue));
1086 //================================================================
1087 // Function : SalomeApp_ComboBox::insertList
1088 /*! Purpose : adds list of items in combo box*/
1089 //================================================================
1090 void SalomeApp_ComboBox::insertList(const TColStd_ListOfInteger& theList)
1092 for (TColStd_ListIteratorOfListOfInteger aIter(theList); aIter.More(); aIter.Next())
1093 insertItem(aIter.Value());
1096 //================================================================
1097 // Function : SalomeApp_ComboBox::insertItem
1098 /*! Purpose : adds item in combo box*/
1099 //================================================================
1100 void SalomeApp_ComboBox::insertItem(const double theValue)
1104 for (int i = 0; i < count(); i++) {
1105 aNum = text(i).toDouble(&bOk);
1107 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1108 insertItem(QString::number(theValue), i);
1113 insertItem(QString::number(theValue));
1116 //================================================================
1117 // Function : SalomeApp_ComboBox::insertList
1118 /*! Purpose : adds list of items in combo box*/
1119 //================================================================
1120 void SalomeApp_ComboBox::insertList(const TColStd_ListOfReal& theList)
1122 for (TColStd_ListIteratorOfListOfReal aIter(theList); aIter.More(); aIter.Next())
1123 insertItem(aIter.Value());
1126 //////////////////////////////////////////////////////////////////////
1127 // SalomeApp_EntityEdit class implementation
1128 //////////////////////////////////////////////////////////////////////
1130 #include <qlayout.h>
1132 #define MIN_COMBO_WIDTH 1
1133 #define MIN_EDIT_WIDTH 1
1135 //================================================================
1136 // Function : SalomeApp_EntityEdit::SalomeApp_EntityEdit
1137 /*! Purpose : constructor*/
1138 //================================================================
1139 SalomeApp_EntityEdit::SalomeApp_EntityEdit(QWidget* parent,
1150 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1151 SUIT_ResourceMgr* mgr = app ? app->resourceMgr() : NULL;
1153 QHBoxLayout* aTopLayout = new QHBoxLayout(this);
1154 aTopLayout->setAlignment( Qt::AlignTop );
1155 aTopLayout->setSpacing( 0 );
1156 aTopLayout->setMargin( 1 );
1157 if (controlType != etLineEdit &&
1158 controlType != etComboBox &&
1159 controlType != etComboEdit)
1160 controlType = etLineEdit;
1161 if (controlType == etComboBox || controlType == etComboEdit) {
1162 // this is an editable combo box
1163 myCombo = new SalomeApp_ComboBox(controlType == etComboEdit, this);
1164 myCombo->setMinimumSize(MIN_COMBO_WIDTH, 0);
1165 myCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1166 QSizePolicy::Fixed));
1168 myCombo->setInsertionPolicy(QComboBox::NoInsertion);
1169 // no duplicates enabled by default
1170 myCombo->setDuplicatesEnabled(false);
1171 aTopLayout->addWidget(myCombo);
1173 connect(myCombo, SIGNAL(activated(const QString&)), this, SLOT(onComboActivated(const QString&)));
1174 connect(myCombo, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1177 // and this is an edit box
1178 myEdit = new SalomeApp_EditBox(this);
1179 myEdit->setMinimumSize(MIN_EDIT_WIDTH, 0);
1180 myEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1181 QSizePolicy::Fixed));
1182 aTopLayout->addWidget(myEdit);
1183 connect(myEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1184 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onApply()));
1185 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onCancel()));
1187 if (valueType != vtString &&
1188 valueType != vtInteger &&
1189 valueType != vtDouble)
1190 valueType = vtString;
1191 if (valueType == vtInteger)
1192 setValidator(new QIntValidator(this));
1193 else if (valueType == vtDouble)
1194 setValidator(new QDoubleValidator(this));
1197 myApplyBtn = new QToolButton(this);
1201 anIcon = mgr->loadPixmap( "STD", tr( "ICON_APPLY" ), false );
1203 myApplyBtn->setPixmap(anIcon);
1204 myApplyBtn->setEnabled(false);
1205 myApplyBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1206 myApplyBtn->setMinimumSize(16, 16);
1207 myApplyBtn->setMaximumSize(16, 20);
1208 aTopLayout->addWidget(myApplyBtn);
1209 connect(myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
1212 // Cancel button (X)
1213 myCancelBtn = new QToolButton(this);
1216 anIcon = mgr->loadPixmap( "STD", tr( "ICON_CANCEL" ), false );
1217 myCancelBtn->setPixmap(anIcon);
1218 myCancelBtn->setEnabled(false);
1219 myCancelBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1220 myCancelBtn->setMinimumSize(16, 16);
1221 myCancelBtn->setMaximumSize(16, 20);
1222 aTopLayout->addWidget(myCancelBtn);
1223 connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(onCancel()));
1227 //================================================================
1228 // Function : SalomeApp_EntityEdit::~SalomeApp_EntityEdit
1229 /*! Purpose : destructor*/
1230 //================================================================
1231 SalomeApp_EntityEdit::~SalomeApp_EntityEdit()
1235 //================================================================
1236 // Function : SalomeApp_EntityEdit::clear
1237 /*! Purpose : clears edit/combo box*/
1238 //================================================================
1239 void SalomeApp_EntityEdit::clear()
1247 //================================================================
1248 // Function : SalomeApp_EntityEdit::getText
1249 /*! Purpose : returns current text in edit box or combo box*/
1250 //================================================================
1251 QString SalomeApp_EntityEdit::getText()
1254 return myEdit->text();
1256 return myCombo->currentText();
1261 //================================================================
1262 // Function : SalomeApp_EntityEdit::setText
1263 /*! Purpose : sets text*/
1264 //================================================================
1265 void SalomeApp_EntityEdit::setText(const QString& theText)
1269 myEdit->setText(theText);
1271 int aFound = myCombo->findItem(theText);
1273 myCombo->setCurrentItem(aFound);
1274 onTextChanged(theText);
1279 //================================================================
1280 // Function : SalomeApp_EntityEdit::insertItem
1281 /*! Purpose : adds item in combo box,
1282 * sets it current if theSetCurrent is true
1284 //================================================================
1285 void SalomeApp_EntityEdit::insertItem(const QString& theValue,
1291 if (theOrder == atTop)
1293 else if (theOrder == atBeforeCurrent && myCombo->count() > 0)
1294 aIndexAt = myCombo->currentItem();
1295 else if (theOrder == atAfterCurrent &&
1296 myCombo->count() > 0 &&
1297 myCombo->currentItem() < myCombo->count()-1)
1298 aIndexAt = myCombo->currentItem() + 1;
1299 myCombo->insertItem(theValue, aIndexAt);
1305 //================================================================
1306 // Function : SalomeApp_EntityEdit::insertList
1307 /*! Purpose : adds items in combo box,
1308 * sets item theCurrent as current
1310 //================================================================
1311 void SalomeApp_EntityEdit::insertList(const QStringList& theList,
1312 const int theCurrent)
1315 myCombo->insertList(theList);
1316 if (theCurrent >= 0 && theCurrent < (int)theList.count())
1317 setText(theList[theCurrent]);
1320 //================================================================
1321 // Function : SalomeApp_EntityEdit::insertItem
1322 /*! Purpose : adds item in combo box,
1323 * sets it current if theSetCurrent is true
1325 //================================================================
1326 void SalomeApp_EntityEdit::insertItem(const int theValue,
1330 myCombo->insertItem(theValue);
1333 setText(QString::number(theValue));
1336 //================================================================
1337 // Function : SalomeApp_EntityEdit::insertList
1338 /*! Purpose : adds items in combo box,
1339 * sets item theCurrent as current
1341 //================================================================
1342 void SalomeApp_EntityEdit::insertList(const TColStd_ListOfInteger& theList,
1343 const int theCurrent)
1346 myCombo->insertList(theList);
1348 TColStd_ListIteratorOfListOfInteger aIter(theList);
1349 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1350 if (theCurrent == i) {
1351 setText(QString::number(aIter.Value()));
1357 //================================================================
1358 // Function : SalomeApp_EntityEdit::insertItem
1359 // Purpose : adds item in combo box,
1360 // sets it current if theSetCurrent is true
1361 //================================================================
1362 void SalomeApp_EntityEdit::insertItem(const double theValue,
1366 myCombo->insertItem(theValue);
1369 setText(QString::number(theValue));
1372 //================================================================
1373 // Function : SalomeApp_EntityEdit::insertList
1374 /*! Purpose : adds items in combo box,
1375 * sets item theCurrent as current
1377 //================================================================
1378 void SalomeApp_EntityEdit::insertList(const TColStd_ListOfReal& theList,
1379 const int theCurrent)
1382 myCombo->insertList(theList);
1384 TColStd_ListIteratorOfListOfReal aIter(theList);
1385 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1386 if (theCurrent == i) {
1387 setText(QString::number(aIter.Value()));
1393 //================================================================
1394 // Function : SalomeApp_EntityEdit::getControl
1395 /*! Purpose : gets actual widget*/
1396 //================================================================
1397 QWidget* SalomeApp_EntityEdit::getControl()
1407 //================================================================
1408 // Function : SalomeApp_EntityEdit::setFocus
1409 /*! Purpose : redirect focus to corresponding widget*/
1410 //================================================================
1411 void SalomeApp_EntityEdit::setFocus()
1415 //myEdit->selectAll();
1417 else if (myCombo && myCombo->editable()) {
1418 myCombo->setFocus();
1419 //myCombo->lineEdit()->selectAll();
1423 //================================================================
1424 // Function : SalomeApp_EntityEdit::setValidator
1425 /*! Purpose : sets validator for the control*/
1426 //================================================================
1427 void SalomeApp_EntityEdit::setValidator(const QValidator* theValidator)
1430 myEdit->setValidator(theValidator);
1432 myCombo->setValidator(theValidator);
1435 //================================================================
1436 // Function : SalomeApp_EntityEdit::keyPressEvent
1437 /*! Purpose : event filter for KeyPress event*/
1438 //================================================================
1439 void SalomeApp_EntityEdit::keyPressEvent( QKeyEvent * e)
1441 if ( (e->key() == Key_Enter ||
1442 e->key() == Key_Return ) )
1444 else if (e->key() == Key_Escape)
1448 //================================================================
1449 // Function : SalomeApp_EntityEdit::onComboActivated
1450 /*! Purpose : called when item activated in combo box*/
1451 //================================================================
1452 void SalomeApp_EntityEdit::onComboActivated(const QString& theText)
1454 onTextChanged(theText);
1457 //================================================================
1458 // Function : SalomeApp_EntityEdit::onTextChanged
1459 /*! Purpose : slot, called when text changed in line edit*/
1460 //================================================================
1461 void SalomeApp_EntityEdit::onTextChanged(const QString& theText)
1464 myApplyBtn->setEnabled(!(theText == myString));
1466 myCancelBtn->setEnabled(!(theText == myString));
1469 //================================================================
1470 // Function : SalomeApp_EntityEdit::onCancel
1471 /*! Purpose : slot, called when user presses Cancel button*/
1472 //================================================================
1473 void SalomeApp_EntityEdit::onCancel()
1477 myApplyBtn->setEnabled(false);
1479 myCancelBtn->setEnabled(false);
1480 emit escapePressed();
1483 //================================================================
1484 // Function : SalomeApp_EntityEdit::onApply
1485 /*! Purpose : slot, called when user presses Apply button*/
1486 //================================================================
1487 void SalomeApp_EntityEdit::onApply()
1489 myString = getText();
1491 myApplyBtn->setEnabled(false);
1493 myCancelBtn->setEnabled(false);
1494 emit returnPressed();
1497 //================================================================
1498 // Function : SalomeApp_EntityEdit::showButtons
1499 /*! Purpose : shows/hides buttons*/
1500 //================================================================
1501 void SalomeApp_EntityEdit::showButtons(bool show)
1504 show ? myApplyBtn->show() : myApplyBtn->hide();
1506 show ? myCancelBtn->show() : myCancelBtn->hide();
1509 //================================================================
1510 // Function : SalomeApp_EntityEdit::setDuplicatesEnabled
1511 /*! Purpose : enables/disables data duplication (for combo box)*/
1512 //================================================================
1513 void SalomeApp_EntityEdit::setDuplicatesEnabled(bool enabled)
1516 myCombo->setDuplicatesEnabled(enabled);