1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : QAD_ListView.cxx
8 // Author : Vadim SANDLER
13 #include "QAD_ListView.h"
16 #include <qvalidator.h>
17 #include <qapplication.h>
18 #include <qtoolbutton.h>
20 #include "QAD_Desktop.h"
22 //#include <QAD_Application.h>
23 //#include <QAD_Document.h>
26 #include <TColStd_ListIteratorOfListOfInteger.hxx>
27 #include <TColStd_ListIteratorOfListOfReal.hxx>
29 //////////////////////////////////////////////////////////////////////
30 // QAD_ListView class implementation
31 //////////////////////////////////////////////////////////////////////
33 //================================================================
34 // Function : computeEditGeometry
35 // Purpose : static function - used for resizing editing widget
36 //================================================================
37 void computeEditGeometry(QAD_ListViewItem* theItem,
38 QAD_EntityEdit* theWidget)
42 QListView* aListView = theItem->listView();
43 int anEditColumn = theItem->getEditedColumn();
47 int aX = 0, aY = 0, aW = 0, aH = 0;
49 QRect aRect = aListView->itemRect(theItem);
50 aListView->contentsToViewport(aListView->header()->sectionPos(anEditColumn), 0, aX, aY);
52 aX = 0; // THIS CAN BE REMOVED
53 QSize aSize = theWidget->getControl()->sizeHint();
54 aH = QMAX(aSize.height() , aRect.height() );
55 aY = aRect.y() - ((aH - aRect.height()) / 2);
56 //aW = aListView->columnWidth(anEditColumn); // CAN SUBSTITUTE NEXT 3 ROWS
57 aW = aListView->viewport()->width() - aX;
60 theWidget->setGeometry(aX, aY, aW, aH);
63 //================================================================
64 // Function : QAD_ListView::QAD_ListView
65 // Purpose : constructor
66 //================================================================
67 QAD_ListView::QAD_ListView(QWidget *parent) :
71 myMouseEnabled = true;
72 myEditingEnabled = false;
74 setSelectionMode(Single);
76 setRootIsDecorated(false);
77 setAllColumnsShowFocus(false);
78 // header()->setClickEnabled(false);
79 header()->setMovingEnabled(false);
84 viewport()->installEventFilter(this);
86 connect(this, SIGNAL(selectionChanged()),
87 this, SLOT(onSelectionChanged()));
88 connect(header(), SIGNAL(sizeChange(int, int, int)),
89 this, SLOT(onHeaderSizeChange(int, int, int)));
92 //================================================================
93 // Function : QAD_ListView::~QAD_ListView
94 // Purpose : destructor
95 //================================================================
96 QAD_ListView::~QAD_ListView()
107 //================================================================
108 // Function : QAD_ListView::updateViewer
109 // Purpose : updates all data viewer
110 //================================================================
111 void QAD_ListView::updateViewer()
113 // temporary disconnecting selection changed SIGNAL
115 QAD_ListViewItem* aRoot = (QAD_ListViewItem*)firstChild();
117 aRoot->updateAllLevels();
119 // connecting again selection changed SIGNAL
121 emit selectionChanged();
124 //================================================================
125 // Function : QAD_ListView::updateSelected
126 // Purpose : updates currently selected item(s)
127 //================================================================
128 void QAD_ListView::updateSelected()
130 // temporary disconnecting selection changed SIGNAL
132 QAD_ListViewItem* aChild = (QAD_ListViewItem*)selectedItem();
134 aChild->updateAllLevels();
136 // connecting again selection changed SIGNAL
138 emit selectionChanged();
141 //================================================================
142 // Function : QAD_ListView::onCreatePopup
143 // Purpose : fills popup menu with items
144 //================================================================
145 void QAD_ListView::onCreatePopup()
152 //================================================================
153 // Function : QAD_ListView::deletePopupMenu
154 // Purpose : delete popup menu
155 //================================================================
156 void QAD_ListView::deletePopupMenu()
165 //================================================================
166 // Function : QAD_ListView::clear
167 // Purpose : clears view
168 //================================================================
169 void QAD_ListView::clear()
179 //================================================================
180 // Function : QAD_ListView::isMouseEnabled
181 // Purpose : returms true if mouse events are enabled
182 //================================================================
183 bool QAD_ListView::isMouseEnabled()
185 return myMouseEnabled;
188 //================================================================
189 // Function : QAD_ListView::enableMouse
190 // Purpose : enabled/disables mouse events (excluding MouseMove)
191 //================================================================
192 void QAD_ListView::enableMouse(bool enable)
194 myMouseEnabled = enable;
197 //================================================================
198 // Function : QAD_ListView::eventFilter
199 // Purpose : event filter
200 //================================================================
201 bool QAD_ListView::eventFilter(QObject* object, QEvent* event)
203 if (object == viewport() &&
204 (event->type() == QEvent::MouseButtonPress ||
205 event->type() == QEvent::MouseButtonRelease ||
206 event->type() == QEvent::MouseButtonDblClick) &&
210 return QListView::eventFilter(object, event);
213 //================================================================
214 // Function : QAD_ListView::enableEditing
215 // Purpose : setting editing of items availbale/not available
216 //================================================================
217 void QAD_ListView::enableEditing(bool theFlag)
219 myEditingEnabled = theFlag;
220 if (!myEditingEnabled) {
229 //================================================================
230 // Function : QAD_ListView::isEnableEditing
231 // Purpose : says if editing is enabled
232 //================================================================
233 bool QAD_ListView::isEnableEditing()
235 return myEditingEnabled;
238 //================================================================
239 // Function : QAD_ListView::accept
240 // Purpose : calls finishEditing(true)...
241 //================================================================
242 void QAD_ListView::accept()
247 //================================================================
248 // Function : QAD_ListView::contentsMouseReleaseEvent
249 // Purpose : mouse release button event
250 //================================================================
251 void QAD_ListView::contentsMouseReleaseEvent(QMouseEvent* e)
253 QListView::contentsMouseReleaseEvent(e);
254 // destroy popup menu if exist
255 qApp->processEvents();
256 // create popup menu on right button click
257 if(e->button() == RightButton) {
260 QPopupMenu* aPopup = createPopup();
263 if (myPopup->count() > 0)
264 //myPopup->exec(QCursor::pos());
265 myPopup->popup(e->globalPos());
270 //================================================================
271 // Function : QAD_ListView::onSelectionChanged
272 // Purpose : slot, called when selection changed in List Viewer
273 //================================================================
274 void QAD_ListView::onSelectionChanged()
280 if (myEditedItem && !myEditedItem->isAccepted()) {
286 // editing is allowed in Single Selection Mode only
287 if (selectionMode() != Single || !isEnableEditing())
289 QAD_ListViewItem* anItem = (QAD_ListViewItem*)selectedItem();
291 if (!anItem->isEditable())
293 myEdit = anItem->startEditing();
295 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
296 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
297 myEditedItem = anItem;
304 //================================================================
305 // Function : QAD_ListView::resizeEvent
306 // Purpose : called when Data Viewer is resized
307 //================================================================
308 void QAD_ListView::resizeEvent( QResizeEvent * e)
310 QListView::resizeEvent(e);
311 int aW = columnWidth(columns()-1);
312 int aX = header()->sectionPos(columns()-1);
313 if (aW < width() - frameWidth() * 2 - aX - 1)
314 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
318 //================================================================
319 // Function : QAD_ListView::onHeaderSizeChange
320 // Purpose : slot, called when columns sizes are changed
321 //================================================================
322 void QAD_ListView::onHeaderSizeChange(int, int, int)
324 int aW = columnWidth(columns()-1);
325 int aX = header()->sectionPos(columns()-1);
326 if (aW < width() - frameWidth() * 2 - aX - 1)
327 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
330 //================================================================
331 // Function : QAD_ListView::viewportPaintEvent
332 // Purpose : handler for paint event
333 //================================================================
334 void QAD_ListView::viewportPaintEvent(QPaintEvent* e)
336 QListView::viewportPaintEvent(e);
337 if (myEditedItem && myEdit) {
338 computeEditGeometry(myEditedItem, myEdit);
342 //================================================================
343 // Function : QAD_ListView::onEditOk
344 // Purpose : called when user finishes in editing of item
345 //================================================================
346 void QAD_ListView::onEditOk()
351 //================================================================
352 // Function : QAD_ListView::onEditCancel
353 // Purpose : called when user cancels item editing
354 //================================================================
355 void QAD_ListView::onEditCancel()
357 finishEditing(false);
360 //================================================================
361 // Function : QAD_ListView::finishEditing
362 // Purpose : finishes editing of entity
363 //================================================================
364 UpdateType QAD_ListView::finishEditing(bool ok)
366 UpdateType aNeedsUpdate = utCancel;
367 if (myEditedItem && myEdit)
369 disconnect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
370 disconnect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
371 myEditedItem->setAccepted(true);
373 aNeedsUpdate = myEditedItem->finishEditing(myEdit);
374 if (aNeedsUpdate == utCancel) {
375 // something to do here on Cancel...
378 // something to do here on OK...
381 switch (aNeedsUpdate) {
385 myEditedItem->updateAllLevels();
391 QAD_ListViewItem* aParent = (QAD_ListViewItem*)(myEditedItem->parent());
393 aParent->updateAllLevels();
395 myEditedItem->updateAllLevels();
406 // doing the same as for utUpdateViewer here
407 // descendants can add extra processing
417 // hide <myEdit> widget
425 //================================================================
426 // Function : QAD_ListView::tip
427 // Purpose : gets current tooltip for list view
428 // returns valid rect in success
429 //================================================================
430 QRect QAD_ListView::tip(QPoint aPos,
435 QRect result( -1, -1, -1, -1 );
436 QAD_ListViewItem* aItem = (QAD_ListViewItem*)itemAt( aPos );
438 for (int i = 0; i < columns(); i++) {
439 QRect aItemRect = aItem->itemRect(i);
440 QRect aTextRect = aItem->textRect(i);
441 if ( !aItem->text(i).isEmpty() &&
442 ( aItemRect.width() > header()->sectionSize(i) ||
443 aTextRect.left() < 0 ||
444 aTextRect.top() < 0 ||
445 aTextRect.right() > viewport()->width() ||
446 aTextRect.bottom() > viewport()->height() ) ) {
447 // calculating tip data
448 aText = aItem->tipText();
449 dspRect = aItem->tipRect();
451 if (dspRect.isValid()) {
452 result = QRect(QPoint(0, aItemRect.top()),
453 QSize(viewport()->width(), aItemRect.height()));
461 //////////////////////////////////////////////////////////////////////
462 // QAD_ListViewItem Class Implementation
463 //////////////////////////////////////////////////////////////////////
465 //================================================================
466 // Function : QAD_ListViewItem::QAD_ListViewItem
467 // Purpose : constructor
468 //================================================================
469 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent) :
470 QListViewItem( parent )
475 //================================================================
476 // Function : QAD_ListViewItem::QAD_ListViewItem
477 // Purpose : constructor
478 //================================================================
479 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
480 QAD_ListViewItem* after) :
481 QListViewItem( parent, after )
486 //================================================================
487 // Function : QAD_ListViewItem::QAD_ListViewItem
488 // Purpose : constructor
489 //================================================================
490 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
491 const QString theName,
492 const bool theEditable) :
493 QListViewItem(parent, theName)
496 setEditable(theEditable);
499 //================================================================
500 // Function : QAD_ListViewItem::QAD_ListViewItem
501 // Purpose : constructor
502 //================================================================
503 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
504 const QString theName,
505 const QString theValue,
506 const bool theEditable) :
507 QListViewItem(parent, theName, theValue)
510 setEditable(theEditable);
513 //================================================================
514 // Function : QAD_ListViewItem::QAD_ListViewItem
515 // Purpose : constructor
516 //================================================================
517 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
518 const QString theName,
519 const bool theEditable) :
520 QListViewItem(parent, theName)
523 setEditable(theEditable);
526 //================================================================
527 // Function : QAD_ListViewItem::QAD_ListViewItem
528 // Purpose : constructor
529 //================================================================
530 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
531 QAD_ListViewItem* after,
532 const QString theName,
533 const bool theEditable) :
534 QListViewItem(parent, after, theName)
537 setEditable(theEditable);
540 //================================================================
541 // Function : QAD_ListViewItem::QAD_ListViewItem
542 // Purpose : constructor
543 //================================================================
544 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
545 QAD_ListViewItem* after,
546 const QString theName,
547 const bool theEditable) :
548 QListViewItem(parent, after, theName)
551 setEditable(theEditable);
555 //================================================================
556 // Function : QAD_ListViewItem::QAD_ListViewItem
557 // Purpose : constructor
558 //================================================================
559 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
560 const QString theName,
561 const QString theValue,
562 const bool theEditable) :
563 QListViewItem(parent, theName, theValue)
566 setEditable(theEditable);
570 //================================================================
571 // Function : QAD_ListViewItem::QAD_ListViewItem
572 // Purpose : constructor
573 //================================================================
574 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
575 QAD_ListViewItem* after,
576 const QString theName,
577 const QString theValue,
578 const bool theEditable) :
579 QListViewItem(parent, after, theName, theValue)
582 setEditable(theEditable);
585 //================================================================
586 // Function : QAD_ListViewItem::QAD_ListViewItem
587 // Purpose : constructor
588 //================================================================
589 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
590 QAD_ListViewItem* after,
591 const QString theName,
592 const QString theValue,
593 const bool theEditable) :
594 QListViewItem(parent, after, theName, theValue)
597 setEditable(theEditable);
600 //================================================================
601 // Function : QAD_ListViewItem::~QAD_ListViewItem
602 // Purpose : destructor
603 //================================================================
604 QAD_ListViewItem::~QAD_ListViewItem()
608 //================================================================
609 // Function : QAD_ListViewItem::init
610 // Purpose : initialization
611 //================================================================
612 void QAD_ListViewItem::init()
616 myEditingType = (int)QAD_EntityEdit::etLineEdit;
617 myValueType = (int)QAD_EntityEdit::vtString;
622 //================================================================
623 // Function : QAD_ListViewItem::getName
624 // Purpose : as default returns text in the first column
625 //================================================================
626 QString QAD_ListViewItem::getName() const
628 return ( listView()->columns() > 0 ) ? text(0) : QString("");
631 //================================================================
632 // Function : QAD_ListViewItem::setName
633 // Purpose : as default sets text in the first column
634 //================================================================
635 UpdateType QAD_ListViewItem::setName(const QString theName)
637 UpdateType aNeedsUpdate = utCancel;
638 if (listView()->columns() > 0) {
640 aNeedsUpdate = utNone;
645 //================================================================
646 // Function : QAD_ListViewItem::getValue
647 // Purpose : as default returns text in the second column
648 //================================================================
649 QString QAD_ListViewItem::getValue() const
651 return ( listView()->columns() > 1 ) ? text(1) : QString("");
654 //================================================================
655 // Function : QAD_ListViewItem::setValue
656 // Purpose : as default sets text in the second column
657 //================================================================
658 UpdateType QAD_ListViewItem::setValue(const QString theValue)
660 UpdateType aNeedsUpdate = utCancel;
661 if (listView()->columns() > 1) {
662 setText(1, theValue);
663 aNeedsUpdate = utNone;
668 //================================================================
669 // Function : QAD_ListViewItem::fullName
670 // Purpose : returns full path to the entity from the root
671 //================================================================
672 QString QAD_ListViewItem::fullName()
674 QString aFullName = getName();
675 QAD_ListViewItem* aParent = (QAD_ListViewItem*)parent();
676 while(aParent != NULL) {
677 aFullName = aParent->getName() + QString(".") + aFullName;
678 aParent = (QAD_ListViewItem*)(aParent->parent());
683 //================================================================
684 // Function : QAD_ListViewItem::openAllLevels
685 // Purpose : expands all entities beginning from this level
686 //================================================================
687 void QAD_ListViewItem::openAllLevels()
690 QAD_ListViewItem* aChild = (QAD_ListViewItem*)firstChild();
692 aChild->openAllLevels();
693 aChild = (QAD_ListViewItem*)(aChild->nextSibling());
697 //================================================================
698 // Function : QAD_ListViewItem::updateAllLevels
699 // Purpose : update all entites beginning from this level
700 //================================================================
701 void QAD_ListViewItem::updateAllLevels()
703 QAD_ListViewItem* aChild = (QAD_ListViewItem*)firstChild();
705 aChild->updateAllLevels();
706 aChild = (QAD_ListViewItem*)(aChild->nextSibling());
710 //================================================================
711 // Function : QAD_EditBox::isEditable
712 // Purpose : return true if entity is editable
713 //================================================================
714 bool QAD_ListViewItem::isEditable() const
719 //================================================================
720 // Function : QAD_ListViewItem::setEditable
721 // Purpose : sets editable flag fo the entity
722 //================================================================
723 void QAD_ListViewItem::setEditable(bool theEditable)
725 myEditable = theEditable;
728 //================================================================
729 // Function : QAD_ListViewItem::isAccepted
730 // Purpose : returns true if entitiy is accepted after editing
731 //================================================================
732 bool QAD_ListViewItem::isAccepted() const
737 //================================================================
738 // Function : QAD_ListViewItem::setAccepted
739 // Purpose : set entitiy accepted or not after editing
740 //================================================================
741 void QAD_ListViewItem::setAccepted(bool theAccepted)
743 myAccepted = theAccepted;
746 //================================================================
747 // Function : QAD_ListViewItem::getEditingType
748 // Purpose : returns type of edit control (0 - edit box, 1 - combo box,
749 // 2 - editable combo box), default is edit box
750 //================================================================
751 int QAD_ListViewItem::getEditingType()
753 return myEditingType;
756 //================================================================
757 // Function : QAD_ListViewItem::setEditingType
758 // Purpose : sets type of edit control (0 - edit box, 1 - combo box,
759 // 2 - editable combo box), negative value means none
760 //================================================================
761 void QAD_ListViewItem::setEditingType(const int type)
763 myEditingType = type;
766 //================================================================
767 // Function : QAD_ListViewItem::getEditedColumn
768 // Purpose : returns edited column, default is last column
769 // negative value means there are no editable columns
770 //================================================================
771 int QAD_ListViewItem::getEditedColumn()
773 return listView()->columns()-1;
776 //================================================================
777 // Function : QAD_ListViewItem::getValueType
778 // Purpose : returns type of edited value (string, int, double)
780 //================================================================
781 int QAD_ListViewItem::getValueType()
786 //================================================================
787 // Function : QAD_ListViewItem::setValueType
788 // Purpose : sets type of edited value
789 //================================================================
790 void QAD_ListViewItem::setValueType(const int valueType)
792 myValueType = valueType;
795 //================================================================
796 // Function : QAD_ListViewItem::getUserType
797 // Purpose : sets type of edited value
798 //================================================================
799 int QAD_ListViewItem::getUserType()
804 //================================================================
805 // Function : QAD_ListViewItem::setUserType
806 // Purpose : sets type of edited value
807 //================================================================
808 void QAD_ListViewItem::setUserType(const int userType)
810 myUserType = userType;
813 //================================================================
814 // Function : QAD_ListViewItem::getButtons
815 // Purpose : returns buttons for editing widget (Apply (V), Cancel (X))
816 // default is both buttons
817 //================================================================
818 int QAD_ListViewItem::getButtons()
823 //================================================================
824 // Function : QAD_ListViewItem::getButtons
825 // Purpose : sets buttons for editing widget (Apply (V), Cancel (X))
826 //================================================================
827 void QAD_ListViewItem::setButtons(const int buttons)
832 //================================================================
833 // Function : QAD_ListViewItem::startEditing
834 // Purpose : creates control for editing and fills it with values
835 //================================================================
836 QAD_EntityEdit* QAD_ListViewItem::startEditing()
838 QAD_EntityEdit* aWidget = 0;
839 QListView* aListView = listView();
843 int anEditType = getEditingType();
844 int aValueType = getValueType();
845 int aButtons = getButtons();
846 int anEditColumn = getEditedColumn();
847 if (anEditColumn < 0 || anEditType < 0)
849 aWidget = new QAD_EntityEdit(aListView->viewport(),
852 aButtons & QAD_EntityEdit::btApply,
853 aButtons & QAD_EntityEdit::btCancel);
854 computeEditGeometry(this, aWidget);
856 fillWidgetWithValues(aWidget);
861 //================================================================
862 // Function : QAD_ListViewItem::fillWidgetWithValues
863 // Purpose : fills widget with initial values (list or single value)
864 //================================================================
865 void QAD_ListViewItem::fillWidgetWithValues(QAD_EntityEdit* theWidget)
867 int anEditColumn = getEditedColumn();
868 if (theWidget && anEditColumn >= 0 && !text(anEditColumn).isEmpty())
869 theWidget->insertItem(text(anEditColumn), true);
872 //================================================================
873 // Function : QAD_ListViewItem::finishEditing
874 // Purpose : finishes editing of entity
875 //================================================================
876 UpdateType QAD_ListViewItem::finishEditing(QAD_EntityEdit* theWidget)
878 UpdateType aNeedsUpdate = utCancel;
881 int anEditColumn = getEditedColumn();
882 switch (anEditColumn) {
884 aNeedsUpdate = setName(theWidget->getText());
887 aNeedsUpdate = setValue(theWidget->getText());
895 MESSAGE( "System error has been caught - QAD_ListViewItem::finishEditing" )
900 //================================================================
901 // Function : QAD_ListViewItem::tipRect
902 // Purpose : calculates rectangle which should contain item's tip
903 //================================================================
904 QRect QAD_ListViewItem::tipRect()
906 QRect aRect = QRect(-1, -1, -1, -1);
907 QRect aItemRect = listView()->itemRect(this);
908 if ( !aItemRect.isValid() )
911 QString aTip = tipText();
912 if (!aTip.isEmpty()) {
913 QRect aRect0 = textRect(0);
914 QFont aFont(listView()->font());
915 QFontMetrics fm(aFont);
916 int iw = fm.width(aTip);
917 aRect = QRect(QPoint(aRect0.x() < 0 ? 0 : aRect0.x(),
925 //================================================================
926 // Function : QAD_ListViewItem::tipText
927 // Purpose : returns text for tooltip
928 //================================================================
929 QString QAD_ListViewItem::tipText()
931 QString aText = getName();
932 if (!getValue().isEmpty())
933 aText += QString(" : ") + getValue();
937 //================================================================
938 // Function : QAD_ListViewItem::textRect
939 // Purpose : calculates rect of item text in viewport coordinates
940 //================================================================
941 QRect QAD_ListViewItem::textRect(const int column) const
943 QRect aItemRect = listView()->itemRect( this );
944 if ( !aItemRect.isValid() )
947 QFont aFont(listView()->font());
948 QFontMetrics fm(aFont);
950 int decorWidth = ( listView()->rootIsDecorated() ) ?
951 ( listView()->treeStepSize() * (depth() + 1) ) :
952 ( listView()->treeStepSize() * depth() );
953 int pixmapWidth = ( pixmap(column) ) ?
954 pixmap(column)->width() + listView()->itemMargin() * 2 :
955 listView()->itemMargin();
957 for (int i = 0; i < column; i++)
958 prevWidth += listView()->header()->sectionSize(i);
961 ((column == 0) ? decorWidth : 0);
962 int iy = aItemRect.y();
963 int iw = fm.width(text(column));
964 int ih = aItemRect.height();
965 if (pixmap(column)) {
966 iy += listView()->itemMargin();
967 ih -= listView()->itemMargin() * 2;
969 ix -= listView()->contentsX();
971 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
975 //================================================================
976 // Function : QAD_ListViewItem::itemRect
977 // Purpose : calculates rect of item data in viewport coordinates
978 //================================================================
979 QRect QAD_ListViewItem::itemRect(const int column) const
981 QRect aItemRect = listView()->itemRect( this );
982 if ( !aItemRect.isValid() )
985 QFont aFont(listView()->font());
986 QFontMetrics fm(aFont);
988 int decorWidth = ( listView()->rootIsDecorated() ) ?
989 ( listView()->treeStepSize() * (depth() + 1) ) :
990 ( listView()->treeStepSize() * depth() );
991 int pixmapWidth = ( pixmap(column) ) ?
992 pixmap(column)->width() + listView()->itemMargin() * 2 :
995 for (int i = 0; i < column; i++)
996 prevWidth += listView()->header()->sectionSize(i);
998 int iy = aItemRect.y();
999 int iw = pixmapWidth +
1000 listView()->itemMargin() * 2 +
1001 ((column == 0) ? decorWidth : 0) +
1002 fm.width(text(column));
1003 int ih = aItemRect.height();
1004 ix -= listView()->contentsX();
1006 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
1010 //////////////////////////////////////////////////////////////////////
1011 // QAD_EditBox class implementation
1012 //////////////////////////////////////////////////////////////////////
1014 //================================================================
1015 // Function : QAD_EditBox::QAD_EditBox
1016 // Purpose : constructor
1017 //================================================================
1018 QAD_EditBox::QAD_EditBox(QWidget* parent) :
1023 //================================================================
1024 // Function : QAD_EditBox::keyPressEvent
1025 // Purpose : event filter for key pressing
1026 //================================================================
1027 void QAD_EditBox::keyPressEvent( QKeyEvent *e )
1029 if ( e->key() == Key_Escape )
1030 emit escapePressed();
1032 QLineEdit::keyPressEvent( e );
1036 //////////////////////////////////////////////////////////////////////
1037 // QAD_ComboBox class implementation
1038 //////////////////////////////////////////////////////////////////////
1040 //================================================================
1041 // Function : QAD_ComboBox::QAD_ComboBox
1042 // Purpose : constructor
1043 //================================================================
1044 QAD_ComboBox::QAD_ComboBox(bool rw, QWidget* parent, const char* name) :
1045 QComboBox(rw, parent, name)
1049 //================================================================
1050 // Function : QAD_ComboBox::findItem
1051 // Purpose : searches item in list and returns its index
1052 //================================================================
1053 int QAD_ComboBox::findItem(const QString theText)
1055 for (int i = 0; i < count(); i++)
1056 if (text(i) == theText)
1061 //================================================================
1062 // Function : QAD_ComboBox::insertItem
1063 // Purpose : adds item in combo box
1064 //================================================================
1065 void QAD_ComboBox::insertItem(const QString& theValue,
1068 if (duplicatesEnabled() || findItem(theValue) < 0)
1069 QComboBox::insertItem(theValue, theIndex);
1072 //================================================================
1073 // Function : QAD_ComboBox::insertList
1074 // Purpose : adds list of items in combo box
1075 //================================================================
1076 void QAD_ComboBox::insertList(const QStringList& theList)
1078 for (unsigned i = 0; i < theList.count(); i++)
1079 insertItem(theList[i]);
1082 //================================================================
1083 // Function : QAD_ComboBox::insertItem
1084 // Purpose : adds item in combo box
1085 //================================================================
1086 void QAD_ComboBox::insertItem(const int theValue)
1090 for (int i = 0; i < count(); i++) {
1091 aNum = text(i).toInt(&bOk);
1093 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1094 insertItem(QString::number(theValue), i);
1099 insertItem(QString::number(theValue));
1102 //================================================================
1103 // Function : QAD_ComboBox::insertList
1104 // Purpose : adds list of items in combo box
1105 //================================================================
1106 void QAD_ComboBox::insertList(const TColStd_ListOfInteger& theList)
1108 for (TColStd_ListIteratorOfListOfInteger aIter(theList); aIter.More(); aIter.Next())
1109 insertItem(aIter.Value());
1112 //================================================================
1113 // Function : QAD_ComboBox::insertItem
1114 // Purpose : adds item in combo box
1115 //================================================================
1116 void QAD_ComboBox::insertItem(const double theValue)
1120 for (int i = 0; i < count(); i++) {
1121 aNum = text(i).toDouble(&bOk);
1123 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1124 insertItem(QString::number(theValue), i);
1129 insertItem(QString::number(theValue));
1132 //================================================================
1133 // Function : QAD_ComboBox::insertList
1134 // Purpose : adds list of items in combo box
1135 //================================================================
1136 void QAD_ComboBox::insertList(const TColStd_ListOfReal& theList)
1138 for (TColStd_ListIteratorOfListOfReal aIter(theList); aIter.More(); aIter.Next())
1139 insertItem(aIter.Value());
1142 //////////////////////////////////////////////////////////////////////
1143 // QAD_EntityEdit class implementation
1144 //////////////////////////////////////////////////////////////////////
1146 #include <qlayout.h>
1148 #define MIN_COMBO_WIDTH 1
1149 #define MIN_EDIT_WIDTH 1
1151 //================================================================
1152 // Function : QAD_EntityEdit::QAD_EntityEdit
1153 // Purpose : constructor
1154 //================================================================
1155 QAD_EntityEdit::QAD_EntityEdit(QWidget* parent,
1166 QHBoxLayout* aTopLayout = new QHBoxLayout(this);
1167 aTopLayout->setAlignment( Qt::AlignTop );
1168 aTopLayout->setSpacing( 0 );
1169 aTopLayout->setMargin( 1 );
1170 if (controlType != etLineEdit &&
1171 controlType != etComboBox &&
1172 controlType != etComboEdit)
1173 controlType = etLineEdit;
1174 if (controlType == etComboBox || controlType == etComboEdit) {
1175 // this is an editable combo box
1176 myCombo = new QAD_ComboBox(controlType == etComboEdit, this);
1177 myCombo->setMinimumSize(MIN_COMBO_WIDTH, 0);
1178 myCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1179 QSizePolicy::Fixed));
1181 myCombo->setInsertionPolicy(QComboBox::NoInsertion);
1182 // no duplicates enabled by default
1183 myCombo->setDuplicatesEnabled(false);
1184 aTopLayout->addWidget(myCombo);
1186 connect(myCombo, SIGNAL(activated(const QString&)), this, SLOT(onComboActivated(const QString&)));
1187 connect(myCombo, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1190 // and this is an edit box
1191 myEdit = new QAD_EditBox(this);
1192 myEdit->setMinimumSize(MIN_EDIT_WIDTH, 0);
1193 myEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1194 QSizePolicy::Fixed));
1195 aTopLayout->addWidget(myEdit);
1196 connect(myEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1197 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onApply()));
1198 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onCancel()));
1200 if (valueType != vtString &&
1201 valueType != vtInteger &&
1202 valueType != vtDouble)
1203 valueType = vtString;
1204 if (valueType == vtInteger)
1205 setValidator(new QIntValidator(this));
1206 else if (valueType == vtDouble)
1207 setValidator(new QDoubleValidator(this));
1210 myApplyBtn = new QToolButton(this);
1211 QPixmap anIcon = QAD_Desktop::getResourceManager()->loadPixmap(
1212 "QAD",tr("ICON_APPLY_BTN"));
1213 myApplyBtn->setPixmap(anIcon);
1214 myApplyBtn->setEnabled(false);
1215 myApplyBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1216 myApplyBtn->setMinimumSize(16, 16);
1217 myApplyBtn->setMaximumSize(16, 20);
1218 aTopLayout->addWidget(myApplyBtn);
1219 connect(myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
1222 // Cancel button (X)
1223 myCancelBtn = new QToolButton(this);
1224 QPixmap anIcon = QAD_Desktop::getResourceManager()->loadPixmap(
1225 "QAD",tr("ICON_CANCEL_BTN"));
1226 myCancelBtn->setPixmap(anIcon);
1227 myCancelBtn->setEnabled(false);
1228 myCancelBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1229 myCancelBtn->setMinimumSize(16, 16);
1230 myCancelBtn->setMaximumSize(16, 20);
1231 aTopLayout->addWidget(myCancelBtn);
1232 connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(onCancel()));
1236 //================================================================
1237 // Function : QAD_EntityEdit::~QAD_EntityEdit
1238 // Purpose : destructor
1239 //================================================================
1240 QAD_EntityEdit::~QAD_EntityEdit()
1244 //================================================================
1245 // Function : QAD_EntityEdit::clear
1246 // Purpose : clears edit/combo box
1247 //================================================================
1248 void QAD_EntityEdit::clear()
1256 //================================================================
1257 // Function : QAD_EntityEdit::getText
1258 // Purpose : returns current text in edit box or combo box
1259 //================================================================
1260 QString QAD_EntityEdit::getText()
1263 return myEdit->text();
1265 return myCombo->currentText();
1270 //================================================================
1271 // Function : QAD_EntityEdit::setText
1272 // Purpose : sets text
1273 //================================================================
1274 void QAD_EntityEdit::setText(const QString& theText)
1278 myEdit->setText(theText);
1280 int aFound = myCombo->findItem(theText);
1282 myCombo->setCurrentItem(aFound);
1283 onTextChanged(theText);
1288 //================================================================
1289 // Function : QAD_EntityEdit::insertItem
1290 // Purpose : adds item in combo box,
1291 // sets it current if theSetCurrent is true
1292 //================================================================
1293 void QAD_EntityEdit::insertItem(const QString& theValue,
1299 if (theOrder == atTop)
1301 else if (theOrder == atBeforeCurrent && myCombo->count() > 0)
1302 aIndexAt = myCombo->currentItem();
1303 else if (theOrder == atAfterCurrent &&
1304 myCombo->count() > 0 &&
1305 myCombo->currentItem() < myCombo->count()-1)
1306 aIndexAt = myCombo->currentItem() + 1;
1307 myCombo->insertItem(theValue, aIndexAt);
1313 //================================================================
1314 // Function : QAD_EntityEdit::insertList
1315 // Purpose : adds items in combo box,
1316 // sets item theCurrent as current
1317 //================================================================
1318 void QAD_EntityEdit::insertList(const QStringList& theList,
1319 const int theCurrent)
1322 myCombo->insertList(theList);
1323 if (theCurrent >= 0 && theCurrent < (int)theList.count())
1324 setText(theList[theCurrent]);
1327 //================================================================
1328 // Function : QAD_EntityEdit::insertItem
1329 // Purpose : adds item in combo box,
1330 // sets it current if theSetCurrent is true
1331 //================================================================
1332 void QAD_EntityEdit::insertItem(const int theValue,
1336 myCombo->insertItem(theValue);
1339 setText(QString::number(theValue));
1342 //================================================================
1343 // Function : QAD_EntityEdit::insertList
1344 // Purpose : adds items in combo box,
1345 // sets item theCurrent as current
1346 //================================================================
1347 void QAD_EntityEdit::insertList(const TColStd_ListOfInteger& theList,
1348 const int theCurrent)
1351 myCombo->insertList(theList);
1353 TColStd_ListIteratorOfListOfInteger aIter(theList);
1354 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1355 if (theCurrent == i) {
1356 setText(QString::number(aIter.Value()));
1362 //================================================================
1363 // Function : QAD_EntityEdit::insertItem
1364 // Purpose : adds item in combo box,
1365 // sets it current if theSetCurrent is true
1366 //================================================================
1367 void QAD_EntityEdit::insertItem(const double theValue,
1371 myCombo->insertItem(theValue);
1374 setText(QString::number(theValue));
1377 //================================================================
1378 // Function : QAD_EntityEdit::insertList
1379 // Purpose : adds items in combo box,
1380 // sets item theCurrent as current
1381 //================================================================
1382 void QAD_EntityEdit::insertList(const TColStd_ListOfReal& theList,
1383 const int theCurrent)
1386 myCombo->insertList(theList);
1388 TColStd_ListIteratorOfListOfReal aIter(theList);
1389 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1390 if (theCurrent == i) {
1391 setText(QString::number(aIter.Value()));
1397 //================================================================
1398 // Function : QAD_EntityEdit::getControl
1399 // Purpose : gets actual widget
1400 //================================================================
1401 QWidget* QAD_EntityEdit::getControl()
1411 //================================================================
1412 // Function : QAD_EntityEdit::setFocus
1413 // Purpose : redirect focus to corresponding widget
1414 //================================================================
1415 void QAD_EntityEdit::setFocus()
1419 //myEdit->selectAll();
1421 else if (myCombo && myCombo->editable()) {
1422 myCombo->setFocus();
1423 //myCombo->lineEdit()->selectAll();
1427 //================================================================
1428 // Function : QAD_EntityEdit::setValidator
1429 // Purpose : sets validator for the control
1430 //================================================================
1431 void QAD_EntityEdit::setValidator(const QValidator* theValidator)
1434 myEdit->setValidator(theValidator);
1436 myCombo->setValidator(theValidator);
1439 //================================================================
1440 // Function : QAD_EntityEdit::keyPressEvent
1441 // Purpose : event filter for KeyPress event
1442 //================================================================
1443 void QAD_EntityEdit::keyPressEvent( QKeyEvent * e)
1445 if ( (e->key() == Key_Enter ||
1446 e->key() == Key_Return ) )
1448 else if (e->key() == Key_Escape)
1452 //================================================================
1453 // Function : QAD_EntityEdit::onComboActivated
1454 // Purpose : called when item activated in combo box
1455 //================================================================
1456 void QAD_EntityEdit::onComboActivated(const QString& theText)
1458 onTextChanged(theText);
1461 //================================================================
1462 // Function : QAD_EntityEdit::onTextChanged
1463 // Purpose : slot, called when text changed in line edit
1464 //================================================================
1465 void QAD_EntityEdit::onTextChanged(const QString& theText)
1468 myApplyBtn->setEnabled(!(theText == myString));
1470 myCancelBtn->setEnabled(!(theText == myString));
1473 //================================================================
1474 // Function : QAD_EntityEdit::onCancel
1475 // Purpose : slot, called when user presses Cancel button
1476 //================================================================
1477 void QAD_EntityEdit::onCancel()
1481 myApplyBtn->setEnabled(false);
1483 myCancelBtn->setEnabled(false);
1484 emit escapePressed();
1487 //================================================================
1488 // Function : QAD_EntityEdit::onApply
1489 // Purpose : slot, called when user presses Apply button
1490 //================================================================
1491 void QAD_EntityEdit::onApply()
1493 myString = getText();
1495 myApplyBtn->setEnabled(false);
1497 myCancelBtn->setEnabled(false);
1498 emit returnPressed();
1501 //================================================================
1502 // Function : QAD_EntityEdit::showButtons
1503 // Purpose : shows/hides buttons
1504 //================================================================
1505 void QAD_EntityEdit::showButtons(bool show)
1508 show ? myApplyBtn->show() : myApplyBtn->hide();
1510 show ? myCancelBtn->show() : myCancelBtn->hide();
1513 //================================================================
1514 // Function : QAD_EntityEdit::setDuplicatesEnabled
1515 // Purpose : enables/disables data duplication (for combo box)
1516 //================================================================
1517 void QAD_EntityEdit::setDuplicatesEnabled(bool enabled)
1520 myCombo->setDuplicatesEnabled(enabled);