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) :
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 // returns valid rect in success
399 //================================================================
400 QRect SalomeApp_ListView::tip(QPoint aPos,
405 QRect result( -1, -1, -1, -1 );
406 SalomeApp_ListViewItem* aItem = (SalomeApp_ListViewItem*)itemAt( aPos );
408 for (int i = 0; i < columns(); i++) {
409 QRect aItemRect = aItem->itemRect(i);
410 QRect aTextRect = aItem->textRect(i);
411 if ( !aItem->text(i).isEmpty() &&
412 ( aItemRect.width() > header()->sectionSize(i) ||
413 aTextRect.left() < 0 ||
414 aTextRect.top() < 0 ||
415 aTextRect.right() > viewport()->width() ||
416 aTextRect.bottom() > viewport()->height() ) ) {
417 // calculating tip data
418 aText = aItem->tipText();
419 dspRect = aItem->tipRect();
421 if (dspRect.isValid()) {
422 result = QRect(QPoint(0, aItemRect.top()),
423 QSize(viewport()->width(), aItemRect.height()));
431 //////////////////////////////////////////////////////////////////////
432 // SalomeApp_ListViewItem Class Implementation
433 //////////////////////////////////////////////////////////////////////
435 //================================================================
436 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
437 // Purpose : constructor
438 //================================================================
439 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent) :
440 QListViewItem( parent )
445 //================================================================
446 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
447 // Purpose : constructor
448 //================================================================
449 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
450 SalomeApp_ListViewItem* after) :
451 QListViewItem( parent, after )
456 //================================================================
457 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
458 // Purpose : constructor
459 //================================================================
460 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
461 const QString& theName,
462 const bool theEditable) :
463 QListViewItem(parent, theName)
466 setEditable(theEditable);
469 //================================================================
470 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
471 // Purpose : constructor
472 //================================================================
473 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
474 const QString& theName,
475 const QString& theValue,
476 const bool theEditable) :
477 QListViewItem(parent, theName, theValue)
480 setEditable(theEditable);
483 //================================================================
484 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
485 // Purpose : constructor
486 //================================================================
487 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
488 const QString& theName,
489 const bool theEditable) :
490 QListViewItem(parent, theName)
493 setEditable(theEditable);
496 //================================================================
497 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
498 // Purpose : constructor
499 //================================================================
500 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
501 SalomeApp_ListViewItem* after,
502 const QString& theName,
503 const bool theEditable) :
504 QListViewItem(parent, after, theName)
507 setEditable(theEditable);
510 //================================================================
511 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
512 // Purpose : constructor
513 //================================================================
514 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
515 SalomeApp_ListViewItem* after,
516 const QString& theName,
517 const bool theEditable) :
518 QListViewItem(parent, after, theName)
521 setEditable(theEditable);
525 //================================================================
526 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
527 // Purpose : constructor
528 //================================================================
529 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
530 const QString& theName,
531 const QString& theValue,
532 const bool theEditable) :
533 QListViewItem(parent, theName, theValue)
536 setEditable(theEditable);
540 //================================================================
541 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
542 // Purpose : constructor
543 //================================================================
544 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListViewItem* parent,
545 SalomeApp_ListViewItem* after,
546 const QString& theName,
547 const QString& theValue,
548 const bool theEditable) :
549 QListViewItem(parent, after, theName, theValue)
552 setEditable(theEditable);
555 //================================================================
556 // Function : SalomeApp_ListViewItem::SalomeApp_ListViewItem
557 // Purpose : constructor
558 //================================================================
559 SalomeApp_ListViewItem::SalomeApp_ListViewItem(SalomeApp_ListView* parent,
560 SalomeApp_ListViewItem* after,
561 const QString& theName,
562 const QString& theValue,
563 const bool theEditable) :
564 QListViewItem(parent, after, theName, theValue)
567 setEditable(theEditable);
570 //================================================================
571 // Function : SalomeApp_ListViewItem::~SalomeApp_ListViewItem
572 // Purpose : destructor
573 //================================================================
574 SalomeApp_ListViewItem::~SalomeApp_ListViewItem()
578 //================================================================
579 // Function : SalomeApp_ListViewItem::init
580 // Purpose : initialization
581 //================================================================
582 void SalomeApp_ListViewItem::init()
586 myEditingType = (int)SalomeApp_EntityEdit::etLineEdit;
587 myValueType = (int)SalomeApp_EntityEdit::vtString;
592 //================================================================
593 // Function : SalomeApp_ListViewItem::getName
594 // Purpose : as default returns text in the first column
595 //================================================================
596 QString SalomeApp_ListViewItem::getName() const
598 return ( listView()->columns() > 0 ) ? text(0) : QString("");
601 //================================================================
602 // Function : SalomeApp_ListViewItem::setName
603 // Purpose : as default sets text in the first column
604 //================================================================
605 UpdateType SalomeApp_ListViewItem::setName(const QString& theName)
607 UpdateType aNeedsUpdate = utCancel;
608 if (listView()->columns() > 0) {
610 aNeedsUpdate = utNone;
615 //================================================================
616 // Function : SalomeApp_ListViewItem::getValue
617 // Purpose : as default returns text in the second column
618 //================================================================
619 QString SalomeApp_ListViewItem::getValue() const
621 return ( listView()->columns() > 1 ) ? text(1) : QString("");
624 //================================================================
625 // Function : SalomeApp_ListViewItem::setValue
626 // Purpose : as default sets text in the second column
627 //================================================================
628 UpdateType SalomeApp_ListViewItem::setValue(const QString& theValue)
630 UpdateType aNeedsUpdate = utCancel;
631 if (listView()->columns() > 1) {
632 setText(1, theValue);
633 aNeedsUpdate = utNone;
638 //================================================================
639 // Function : SalomeApp_ListViewItem::fullName
640 // Purpose : returns full path to the entity from the root
641 //================================================================
642 QString SalomeApp_ListViewItem::fullName()
644 QString aFullName = getName();
645 SalomeApp_ListViewItem* aParent = (SalomeApp_ListViewItem*)parent();
646 while(aParent != NULL) {
647 aFullName = aParent->getName() + QString(".") + aFullName;
648 aParent = (SalomeApp_ListViewItem*)(aParent->parent());
653 //================================================================
654 // Function : SalomeApp_ListViewItem::openAllLevels
655 // Purpose : expands all entities beginning from this level
656 //================================================================
657 void SalomeApp_ListViewItem::openAllLevels()
660 SalomeApp_ListViewItem* aChild = (SalomeApp_ListViewItem*)firstChild();
662 aChild->openAllLevels();
663 aChild = (SalomeApp_ListViewItem*)(aChild->nextSibling());
667 //================================================================
668 // Function : SalomeApp_ListViewItem::updateAllLevels
669 // Purpose : update all entites beginning from this level
670 //================================================================
671 void SalomeApp_ListViewItem::updateAllLevels()
673 SalomeApp_ListViewItem* aChild = (SalomeApp_ListViewItem*)firstChild();
675 aChild->updateAllLevels();
676 aChild = (SalomeApp_ListViewItem*)(aChild->nextSibling());
680 //================================================================
681 // Function : SalomeApp_EditBox::isEditable
682 // Purpose : return true if entity is editable
683 //================================================================
684 bool SalomeApp_ListViewItem::isEditable() const
689 //================================================================
690 // Function : SalomeApp_ListViewItem::setEditable
691 // Purpose : sets editable flag fo the entity
692 //================================================================
693 void SalomeApp_ListViewItem::setEditable(bool theEditable)
695 myEditable = theEditable;
698 //================================================================
699 // Function : SalomeApp_ListViewItem::isAccepted
700 // Purpose : returns true if entitiy is accepted after editing
701 //================================================================
702 bool SalomeApp_ListViewItem::isAccepted() const
707 //================================================================
708 // Function : SalomeApp_ListViewItem::setAccepted
709 // Purpose : set entitiy accepted or not after editing
710 //================================================================
711 void SalomeApp_ListViewItem::setAccepted(bool theAccepted)
713 myAccepted = theAccepted;
716 //================================================================
717 // Function : SalomeApp_ListViewItem::getEditingType
718 // Purpose : returns type of edit control (0 - edit box, 1 - combo box,
719 // 2 - editable combo box), default is edit box
720 //================================================================
721 int SalomeApp_ListViewItem::getEditingType()
723 return myEditingType;
726 //================================================================
727 // Function : SalomeApp_ListViewItem::setEditingType
728 // Purpose : sets type of edit control (0 - edit box, 1 - combo box,
729 // 2 - editable combo box), negative value means none
730 //================================================================
731 void SalomeApp_ListViewItem::setEditingType(const int type)
733 myEditingType = type;
736 //================================================================
737 // Function : SalomeApp_ListViewItem::getEditedColumn
738 // Purpose : returns edited column, default is last column
739 // negative value means there are no editable columns
740 //================================================================
741 int SalomeApp_ListViewItem::getEditedColumn()
743 return listView()->columns()-1;
746 //================================================================
747 // Function : SalomeApp_ListViewItem::getValueType
748 // Purpose : returns type of edited value (string, int, double)
750 //================================================================
751 int SalomeApp_ListViewItem::getValueType()
756 //================================================================
757 // Function : SalomeApp_ListViewItem::setValueType
758 // Purpose : sets type of edited value
759 //================================================================
760 void SalomeApp_ListViewItem::setValueType(const int valueType)
762 myValueType = valueType;
765 //================================================================
766 // Function : SalomeApp_ListViewItem::getUserType
767 // Purpose : sets type of edited value
768 //================================================================
769 int SalomeApp_ListViewItem::getUserType()
774 //================================================================
775 // Function : SalomeApp_ListViewItem::setUserType
776 // Purpose : sets type of edited value
777 //================================================================
778 void SalomeApp_ListViewItem::setUserType(const int userType)
780 myUserType = userType;
783 //================================================================
784 // Function : SalomeApp_ListViewItem::getButtons
785 // Purpose : returns buttons for editing widget (Apply (V), Cancel (X))
786 // default is both buttons
787 //================================================================
788 int SalomeApp_ListViewItem::getButtons()
793 //================================================================
794 // Function : SalomeApp_ListViewItem::getButtons
795 // Purpose : sets buttons for editing widget (Apply (V), Cancel (X))
796 //================================================================
797 void SalomeApp_ListViewItem::setButtons(const int buttons)
802 //================================================================
803 // Function : SalomeApp_ListViewItem::startEditing
804 // Purpose : creates control for editing and fills it with values
805 //================================================================
806 SalomeApp_EntityEdit* SalomeApp_ListViewItem::startEditing()
808 SalomeApp_EntityEdit* aWidget = 0;
809 QListView* aListView = listView();
813 int anEditType = getEditingType();
814 int aValueType = getValueType();
815 int aButtons = getButtons();
816 int anEditColumn = getEditedColumn();
817 if (anEditColumn < 0 || anEditType < 0)
819 aWidget = new SalomeApp_EntityEdit(aListView->viewport(),
822 aButtons & SalomeApp_EntityEdit::btApply,
823 aButtons & SalomeApp_EntityEdit::btCancel);
824 computeEditGeometry(this, aWidget);
826 fillWidgetWithValues(aWidget);
831 //================================================================
832 // Function : SalomeApp_ListViewItem::fillWidgetWithValues
833 // Purpose : fills widget with initial values (list or single value)
834 //================================================================
835 void SalomeApp_ListViewItem::fillWidgetWithValues(SalomeApp_EntityEdit* theWidget)
837 int anEditColumn = getEditedColumn();
838 if (theWidget && anEditColumn >= 0 && !text(anEditColumn).isEmpty())
839 theWidget->insertItem(text(anEditColumn), true);
842 //================================================================
843 // Function : SalomeApp_ListViewItem::finishEditing
844 // Purpose : finishes editing of entity
845 //================================================================
846 UpdateType SalomeApp_ListViewItem::finishEditing(SalomeApp_EntityEdit* theWidget)
848 UpdateType aNeedsUpdate = utCancel;
851 int anEditColumn = getEditedColumn();
852 switch (anEditColumn) {
854 aNeedsUpdate = setName(theWidget->getText());
857 aNeedsUpdate = setValue(theWidget->getText());
865 MESSAGE( "System error has been caught - SalomeApp_ListViewItem::finishEditing" )
870 //================================================================
871 // Function : SalomeApp_ListViewItem::tipRect
872 // Purpose : calculates rectangle which should contain item's tip
873 //================================================================
874 QRect SalomeApp_ListViewItem::tipRect()
876 QRect aRect = QRect(-1, -1, -1, -1);
877 QRect aItemRect = listView()->itemRect(this);
878 if ( !aItemRect.isValid() )
881 QString aTip = tipText();
882 if (!aTip.isEmpty()) {
883 QRect aRect0 = textRect(0);
884 QFont aFont(listView()->font());
885 QFontMetrics fm(aFont);
886 int iw = fm.width(aTip);
887 aRect = QRect(QPoint(aRect0.x() < 0 ? 0 : aRect0.x(),
895 //================================================================
896 // Function : SalomeApp_ListViewItem::tipText
897 // Purpose : returns text for tooltip
898 //================================================================
899 QString SalomeApp_ListViewItem::tipText()
901 QString aText = getName();
902 if (!getValue().isEmpty())
903 aText += QString(" : ") + getValue();
907 //================================================================
908 // Function : SalomeApp_ListViewItem::textRect
909 // Purpose : calculates rect of item text in viewport coordinates
910 //================================================================
911 QRect SalomeApp_ListViewItem::textRect(const int column) const
913 QRect aItemRect = listView()->itemRect( this );
914 if ( !aItemRect.isValid() )
917 QFont aFont(listView()->font());
918 QFontMetrics fm(aFont);
920 int decorWidth = ( listView()->rootIsDecorated() ) ?
921 ( listView()->treeStepSize() * (depth() + 1) ) :
922 ( listView()->treeStepSize() * depth() );
923 int pixmapWidth = ( pixmap(column) ) ?
924 pixmap(column)->width() + listView()->itemMargin() * 2 :
925 listView()->itemMargin();
927 for (int i = 0; i < column; i++)
928 prevWidth += listView()->header()->sectionSize(i);
931 ((column == 0) ? decorWidth : 0);
932 int iy = aItemRect.y();
933 int iw = fm.width(text(column));
934 int ih = aItemRect.height();
935 if (pixmap(column)) {
936 iy += listView()->itemMargin();
937 ih -= listView()->itemMargin() * 2;
939 ix -= listView()->contentsX();
941 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
945 //================================================================
946 // Function : SalomeApp_ListViewItem::itemRect
947 // Purpose : calculates rect of item data in viewport coordinates
948 //================================================================
949 QRect SalomeApp_ListViewItem::itemRect(const int column) const
951 QRect aItemRect = listView()->itemRect( this );
952 if ( !aItemRect.isValid() )
955 QFont aFont(listView()->font());
956 QFontMetrics fm(aFont);
958 int decorWidth = ( listView()->rootIsDecorated() ) ?
959 ( listView()->treeStepSize() * (depth() + 1) ) :
960 ( listView()->treeStepSize() * depth() );
961 int pixmapWidth = ( pixmap(column) ) ?
962 pixmap(column)->width() + listView()->itemMargin() * 2 :
965 for (int i = 0; i < column; i++)
966 prevWidth += listView()->header()->sectionSize(i);
968 int iy = aItemRect.y();
969 int iw = pixmapWidth +
970 listView()->itemMargin() * 2 +
971 ((column == 0) ? decorWidth : 0) +
972 fm.width(text(column));
973 int ih = aItemRect.height();
974 ix -= listView()->contentsX();
976 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
980 //////////////////////////////////////////////////////////////////////
981 // SalomeApp_EditBox class implementation
982 //////////////////////////////////////////////////////////////////////
984 //================================================================
985 // Function : SalomeApp_EditBox::SalomeApp_EditBox
986 // Purpose : constructor
987 //================================================================
988 SalomeApp_EditBox::SalomeApp_EditBox(QWidget* parent) :
993 //================================================================
994 // Function : SalomeApp_EditBox::keyPressEvent
995 // Purpose : event filter for key pressing
996 //================================================================
997 void SalomeApp_EditBox::keyPressEvent( QKeyEvent *e )
999 if ( e->key() == Key_Escape )
1000 emit escapePressed();
1002 QLineEdit::keyPressEvent( e );
1006 //////////////////////////////////////////////////////////////////////
1007 // SalomeApp_ComboBox class implementation
1008 //////////////////////////////////////////////////////////////////////
1010 //================================================================
1011 // Function : SalomeApp_ComboBox::SalomeApp_ComboBox
1012 // Purpose : constructor
1013 //================================================================
1014 SalomeApp_ComboBox::SalomeApp_ComboBox(bool rw, QWidget* parent, const char* name) :
1015 QComboBox(rw, parent, name)
1019 //================================================================
1020 // Function : SalomeApp_ComboBox::findItem
1021 // Purpose : searches item in list and returns its index
1022 //================================================================
1023 int SalomeApp_ComboBox::findItem(const QString& theText)
1025 for (int i = 0; i < count(); i++)
1026 if (text(i) == theText)
1031 //================================================================
1032 // Function : SalomeApp_ComboBox::insertItem
1033 // Purpose : adds item in combo box
1034 //================================================================
1035 void SalomeApp_ComboBox::insertItem(const QString& theValue,
1038 if (duplicatesEnabled() || findItem(theValue) < 0)
1039 QComboBox::insertItem(theValue, theIndex);
1042 //================================================================
1043 // Function : SalomeApp_ComboBox::insertList
1044 // Purpose : adds list of items in combo box
1045 //================================================================
1046 void SalomeApp_ComboBox::insertList(const QStringList& theList)
1048 for (unsigned i = 0; i < theList.count(); i++)
1049 insertItem(theList[i]);
1052 //================================================================
1053 // Function : SalomeApp_ComboBox::insertItem
1054 // Purpose : adds item in combo box
1055 //================================================================
1056 void SalomeApp_ComboBox::insertItem(const int theValue)
1060 for (int i = 0; i < count(); i++) {
1061 aNum = text(i).toInt(&bOk);
1063 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1064 insertItem(QString::number(theValue), i);
1069 insertItem(QString::number(theValue));
1072 //================================================================
1073 // Function : SalomeApp_ComboBox::insertList
1074 // Purpose : adds list of items in combo box
1075 //================================================================
1076 void SalomeApp_ComboBox::insertList(const TColStd_ListOfInteger& theList)
1078 for (TColStd_ListIteratorOfListOfInteger aIter(theList); aIter.More(); aIter.Next())
1079 insertItem(aIter.Value());
1082 //================================================================
1083 // Function : SalomeApp_ComboBox::insertItem
1084 // Purpose : adds item in combo box
1085 //================================================================
1086 void SalomeApp_ComboBox::insertItem(const double theValue)
1090 for (int i = 0; i < count(); i++) {
1091 aNum = text(i).toDouble(&bOk);
1093 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1094 insertItem(QString::number(theValue), i);
1099 insertItem(QString::number(theValue));
1102 //================================================================
1103 // Function : SalomeApp_ComboBox::insertList
1104 // Purpose : adds list of items in combo box
1105 //================================================================
1106 void SalomeApp_ComboBox::insertList(const TColStd_ListOfReal& theList)
1108 for (TColStd_ListIteratorOfListOfReal aIter(theList); aIter.More(); aIter.Next())
1109 insertItem(aIter.Value());
1112 //////////////////////////////////////////////////////////////////////
1113 // SalomeApp_EntityEdit class implementation
1114 //////////////////////////////////////////////////////////////////////
1116 #include <qlayout.h>
1118 #define MIN_COMBO_WIDTH 1
1119 #define MIN_EDIT_WIDTH 1
1121 //================================================================
1122 // Function : SalomeApp_EntityEdit::SalomeApp_EntityEdit
1123 // Purpose : constructor
1124 //================================================================
1125 SalomeApp_EntityEdit::SalomeApp_EntityEdit(QWidget* parent,
1136 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1137 SUIT_ResourceMgr* mgr = app ? app->resourceMgr() : NULL;
1139 QHBoxLayout* aTopLayout = new QHBoxLayout(this);
1140 aTopLayout->setAlignment( Qt::AlignTop );
1141 aTopLayout->setSpacing( 0 );
1142 aTopLayout->setMargin( 1 );
1143 if (controlType != etLineEdit &&
1144 controlType != etComboBox &&
1145 controlType != etComboEdit)
1146 controlType = etLineEdit;
1147 if (controlType == etComboBox || controlType == etComboEdit) {
1148 // this is an editable combo box
1149 myCombo = new SalomeApp_ComboBox(controlType == etComboEdit, this);
1150 myCombo->setMinimumSize(MIN_COMBO_WIDTH, 0);
1151 myCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1152 QSizePolicy::Fixed));
1154 myCombo->setInsertionPolicy(QComboBox::NoInsertion);
1155 // no duplicates enabled by default
1156 myCombo->setDuplicatesEnabled(false);
1157 aTopLayout->addWidget(myCombo);
1159 connect(myCombo, SIGNAL(activated(const QString&)), this, SLOT(onComboActivated(const QString&)));
1160 connect(myCombo, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1163 // and this is an edit box
1164 myEdit = new SalomeApp_EditBox(this);
1165 myEdit->setMinimumSize(MIN_EDIT_WIDTH, 0);
1166 myEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1167 QSizePolicy::Fixed));
1168 aTopLayout->addWidget(myEdit);
1169 connect(myEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1170 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onApply()));
1171 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onCancel()));
1173 if (valueType != vtString &&
1174 valueType != vtInteger &&
1175 valueType != vtDouble)
1176 valueType = vtString;
1177 if (valueType == vtInteger)
1178 setValidator(new QIntValidator(this));
1179 else if (valueType == vtDouble)
1180 setValidator(new QDoubleValidator(this));
1183 myApplyBtn = new QToolButton(this);
1187 anIcon = mgr->loadPixmap( "STD", tr( "ICON_APPLY" ), false );
1189 myApplyBtn->setPixmap(anIcon);
1190 myApplyBtn->setEnabled(false);
1191 myApplyBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1192 myApplyBtn->setMinimumSize(16, 16);
1193 myApplyBtn->setMaximumSize(16, 20);
1194 aTopLayout->addWidget(myApplyBtn);
1195 connect(myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
1198 // Cancel button (X)
1199 myCancelBtn = new QToolButton(this);
1202 anIcon = mgr->loadPixmap( "STD", tr( "ICON_CANCEL" ), false );
1203 myCancelBtn->setPixmap(anIcon);
1204 myCancelBtn->setEnabled(false);
1205 myCancelBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1206 myCancelBtn->setMinimumSize(16, 16);
1207 myCancelBtn->setMaximumSize(16, 20);
1208 aTopLayout->addWidget(myCancelBtn);
1209 connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(onCancel()));
1213 //================================================================
1214 // Function : SalomeApp_EntityEdit::~SalomeApp_EntityEdit
1215 // Purpose : destructor
1216 //================================================================
1217 SalomeApp_EntityEdit::~SalomeApp_EntityEdit()
1221 //================================================================
1222 // Function : SalomeApp_EntityEdit::clear
1223 // Purpose : clears edit/combo box
1224 //================================================================
1225 void SalomeApp_EntityEdit::clear()
1233 //================================================================
1234 // Function : SalomeApp_EntityEdit::getText
1235 // Purpose : returns current text in edit box or combo box
1236 //================================================================
1237 QString SalomeApp_EntityEdit::getText()
1240 return myEdit->text();
1242 return myCombo->currentText();
1247 //================================================================
1248 // Function : SalomeApp_EntityEdit::setText
1249 // Purpose : sets text
1250 //================================================================
1251 void SalomeApp_EntityEdit::setText(const QString& theText)
1255 myEdit->setText(theText);
1257 int aFound = myCombo->findItem(theText);
1259 myCombo->setCurrentItem(aFound);
1260 onTextChanged(theText);
1265 //================================================================
1266 // Function : SalomeApp_EntityEdit::insertItem
1267 // Purpose : adds item in combo box,
1268 // sets it current if theSetCurrent is true
1269 //================================================================
1270 void SalomeApp_EntityEdit::insertItem(const QString& theValue,
1276 if (theOrder == atTop)
1278 else if (theOrder == atBeforeCurrent && myCombo->count() > 0)
1279 aIndexAt = myCombo->currentItem();
1280 else if (theOrder == atAfterCurrent &&
1281 myCombo->count() > 0 &&
1282 myCombo->currentItem() < myCombo->count()-1)
1283 aIndexAt = myCombo->currentItem() + 1;
1284 myCombo->insertItem(theValue, aIndexAt);
1290 //================================================================
1291 // Function : SalomeApp_EntityEdit::insertList
1292 // Purpose : adds items in combo box,
1293 // sets item theCurrent as current
1294 //================================================================
1295 void SalomeApp_EntityEdit::insertList(const QStringList& theList,
1296 const int theCurrent)
1299 myCombo->insertList(theList);
1300 if (theCurrent >= 0 && theCurrent < (int)theList.count())
1301 setText(theList[theCurrent]);
1304 //================================================================
1305 // Function : SalomeApp_EntityEdit::insertItem
1306 // Purpose : adds item in combo box,
1307 // sets it current if theSetCurrent is true
1308 //================================================================
1309 void SalomeApp_EntityEdit::insertItem(const int theValue,
1313 myCombo->insertItem(theValue);
1316 setText(QString::number(theValue));
1319 //================================================================
1320 // Function : SalomeApp_EntityEdit::insertList
1321 // Purpose : adds items in combo box,
1322 // sets item theCurrent as current
1323 //================================================================
1324 void SalomeApp_EntityEdit::insertList(const TColStd_ListOfInteger& theList,
1325 const int theCurrent)
1328 myCombo->insertList(theList);
1330 TColStd_ListIteratorOfListOfInteger aIter(theList);
1331 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1332 if (theCurrent == i) {
1333 setText(QString::number(aIter.Value()));
1339 //================================================================
1340 // Function : SalomeApp_EntityEdit::insertItem
1341 // Purpose : adds item in combo box,
1342 // sets it current if theSetCurrent is true
1343 //================================================================
1344 void SalomeApp_EntityEdit::insertItem(const double theValue,
1348 myCombo->insertItem(theValue);
1351 setText(QString::number(theValue));
1354 //================================================================
1355 // Function : SalomeApp_EntityEdit::insertList
1356 // Purpose : adds items in combo box,
1357 // sets item theCurrent as current
1358 //================================================================
1359 void SalomeApp_EntityEdit::insertList(const TColStd_ListOfReal& theList,
1360 const int theCurrent)
1363 myCombo->insertList(theList);
1365 TColStd_ListIteratorOfListOfReal aIter(theList);
1366 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1367 if (theCurrent == i) {
1368 setText(QString::number(aIter.Value()));
1374 //================================================================
1375 // Function : SalomeApp_EntityEdit::getControl
1376 // Purpose : gets actual widget
1377 //================================================================
1378 QWidget* SalomeApp_EntityEdit::getControl()
1388 //================================================================
1389 // Function : SalomeApp_EntityEdit::setFocus
1390 // Purpose : redirect focus to corresponding widget
1391 //================================================================
1392 void SalomeApp_EntityEdit::setFocus()
1396 //myEdit->selectAll();
1398 else if (myCombo && myCombo->editable()) {
1399 myCombo->setFocus();
1400 //myCombo->lineEdit()->selectAll();
1404 //================================================================
1405 // Function : SalomeApp_EntityEdit::setValidator
1406 // Purpose : sets validator for the control
1407 //================================================================
1408 void SalomeApp_EntityEdit::setValidator(const QValidator* theValidator)
1411 myEdit->setValidator(theValidator);
1413 myCombo->setValidator(theValidator);
1416 //================================================================
1417 // Function : SalomeApp_EntityEdit::keyPressEvent
1418 // Purpose : event filter for KeyPress event
1419 //================================================================
1420 void SalomeApp_EntityEdit::keyPressEvent( QKeyEvent * e)
1422 if ( (e->key() == Key_Enter ||
1423 e->key() == Key_Return ) )
1425 else if (e->key() == Key_Escape)
1429 //================================================================
1430 // Function : SalomeApp_EntityEdit::onComboActivated
1431 // Purpose : called when item activated in combo box
1432 //================================================================
1433 void SalomeApp_EntityEdit::onComboActivated(const QString& theText)
1435 onTextChanged(theText);
1438 //================================================================
1439 // Function : SalomeApp_EntityEdit::onTextChanged
1440 // Purpose : slot, called when text changed in line edit
1441 //================================================================
1442 void SalomeApp_EntityEdit::onTextChanged(const QString& theText)
1445 myApplyBtn->setEnabled(!(theText == myString));
1447 myCancelBtn->setEnabled(!(theText == myString));
1450 //================================================================
1451 // Function : SalomeApp_EntityEdit::onCancel
1452 // Purpose : slot, called when user presses Cancel button
1453 //================================================================
1454 void SalomeApp_EntityEdit::onCancel()
1458 myApplyBtn->setEnabled(false);
1460 myCancelBtn->setEnabled(false);
1461 emit escapePressed();
1464 //================================================================
1465 // Function : SalomeApp_EntityEdit::onApply
1466 // Purpose : slot, called when user presses Apply button
1467 //================================================================
1468 void SalomeApp_EntityEdit::onApply()
1470 myString = getText();
1472 myApplyBtn->setEnabled(false);
1474 myCancelBtn->setEnabled(false);
1475 emit returnPressed();
1478 //================================================================
1479 // Function : SalomeApp_EntityEdit::showButtons
1480 // Purpose : shows/hides buttons
1481 //================================================================
1482 void SalomeApp_EntityEdit::showButtons(bool show)
1485 show ? myApplyBtn->show() : myApplyBtn->hide();
1487 show ? myCancelBtn->show() : myCancelBtn->hide();
1490 //================================================================
1491 // Function : SalomeApp_EntityEdit::setDuplicatesEnabled
1492 // Purpose : enables/disables data duplication (for combo box)
1493 //================================================================
1494 void SalomeApp_EntityEdit::setDuplicatesEnabled(bool enabled)
1497 myCombo->setDuplicatesEnabled(enabled);