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
12 #include "QAD_ListView.h"
15 #include <qvalidator.h>
16 #include <qapplication.h>
17 #include <qtoolbutton.h>
19 #include "QAD_Desktop.h"
21 #include <TColStd_ListIteratorOfListOfInteger.hxx>
22 #include <TColStd_ListIteratorOfListOfReal.hxx>
24 #include "utilities.h"
28 //////////////////////////////////////////////////////////////////////
29 // QAD_ListView class implementation
30 //////////////////////////////////////////////////////////////////////
32 //================================================================
33 // Function : computeEditGeometry
34 // Purpose : static function - used for resizing editing widget
35 //================================================================
36 void computeEditGeometry(QAD_ListViewItem* theItem,
37 QAD_EntityEdit* theWidget)
41 QListView* aListView = theItem->listView();
42 int anEditColumn = theItem->getEditedColumn();
46 int aX = 0, aY = 0, aW = 0, aH = 0;
48 QRect aRect = aListView->itemRect(theItem);
49 aListView->contentsToViewport(aListView->header()->sectionPos(anEditColumn), 0, aX, aY);
51 aX = 0; // THIS CAN BE REMOVED
52 QSize aSize = theWidget->getControl()->sizeHint();
53 aH = QMAX(aSize.height() , aRect.height() );
54 aY = aRect.y() - ((aH - aRect.height()) / 2);
55 //aW = aListView->columnWidth(anEditColumn); // CAN SUBSTITUTE NEXT 3 ROWS
56 aW = aListView->viewport()->width() - aX;
59 theWidget->setGeometry(aX, aY, aW, aH);
62 //================================================================
63 // Function : QAD_ListView::QAD_ListView
64 // Purpose : constructor
65 //================================================================
66 QAD_ListView::QAD_ListView(QWidget *parent) :
70 myMouseEnabled = true;
71 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 : QAD_ListView::~QAD_ListView
93 // Purpose : destructor
94 //================================================================
95 QAD_ListView::~QAD_ListView()
106 //================================================================
107 // Function : QAD_ListView::updateViewer
108 // Purpose : updates all data viewer
109 //================================================================
110 void QAD_ListView::updateViewer()
112 // temporary disconnecting selection changed SIGNAL
114 QAD_ListViewItem* aRoot = (QAD_ListViewItem*)firstChild();
116 aRoot->updateAllLevels();
118 // connecting again selection changed SIGNAL
120 emit selectionChanged();
123 //================================================================
124 // Function : QAD_ListView::updateSelected
125 // Purpose : updates currently selected item(s)
126 //================================================================
127 void QAD_ListView::updateSelected()
129 // temporary disconnecting selection changed SIGNAL
131 QAD_ListViewItem* aChild = (QAD_ListViewItem*)selectedItem();
133 aChild->updateAllLevels();
135 // connecting again selection changed SIGNAL
137 emit selectionChanged();
140 //================================================================
141 // Function : QAD_ListView::onCreatePopup
142 // Purpose : fills popup menu with items
143 //================================================================
144 void QAD_ListView::onCreatePopup()
151 //================================================================
152 // Function : QAD_ListView::deletePopupMenu
153 // Purpose : delete popup menu
154 //================================================================
155 void QAD_ListView::deletePopupMenu()
164 //================================================================
165 // Function : QAD_ListView::clear
166 // Purpose : clears view
167 //================================================================
168 void QAD_ListView::clear()
178 //================================================================
179 // Function : QAD_ListView::isMouseEnabled
180 // Purpose : returms true if mouse events are enabled
181 //================================================================
182 bool QAD_ListView::isMouseEnabled()
184 return myMouseEnabled;
187 //================================================================
188 // Function : QAD_ListView::enableMouse
189 // Purpose : enabled/disables mouse events (excluding MouseMove)
190 //================================================================
191 void QAD_ListView::enableMouse(bool enable)
193 myMouseEnabled = enable;
196 //================================================================
197 // Function : QAD_ListView::eventFilter
198 // Purpose : event filter
199 //================================================================
200 bool QAD_ListView::eventFilter(QObject* object, QEvent* event)
202 if (object == viewport() &&
203 (event->type() == QEvent::MouseButtonPress ||
204 event->type() == QEvent::MouseButtonRelease ||
205 event->type() == QEvent::MouseButtonDblClick) &&
209 return QListView::eventFilter(object, event);
212 //================================================================
213 // Function : QAD_ListView::enableEditing
214 // Purpose : setting editing of items availbale/not available
215 //================================================================
216 void QAD_ListView::enableEditing(bool theFlag)
218 myEditingEnabled = theFlag;
219 if (!myEditingEnabled) {
228 //================================================================
229 // Function : QAD_ListView::isEnableEditing
230 // Purpose : says if editing is enabled
231 //================================================================
232 bool QAD_ListView::isEnableEditing()
234 return myEditingEnabled;
237 //================================================================
238 // Function : QAD_ListView::accept
239 // Purpose : calls finishEditing(true)...
240 //================================================================
241 void QAD_ListView::accept()
246 //================================================================
247 // Function : QAD_ListView::contentsMouseReleaseEvent
248 // Purpose : mouse release button event
249 //================================================================
250 void QAD_ListView::contentsMouseReleaseEvent(QMouseEvent* e)
252 QListView::contentsMouseReleaseEvent(e);
253 // destroy popup menu if exist
254 qApp->processEvents();
255 // create popup menu on right button click
256 if(e->button() == RightButton) {
259 QPopupMenu* aPopup = createPopup();
262 if (myPopup->count() > 0)
263 //myPopup->exec(QCursor::pos());
264 myPopup->popup(e->globalPos());
269 //================================================================
270 // Function : QAD_ListView::onSelectionChanged
271 // Purpose : slot, called when selection changed in List Viewer
272 //================================================================
273 void QAD_ListView::onSelectionChanged()
279 if (myEditedItem && !myEditedItem->isAccepted()) {
285 // editing is allowed in Single Selection Mode only
286 if (selectionMode() != Single || !isEnableEditing())
288 QAD_ListViewItem* anItem = (QAD_ListViewItem*)selectedItem();
290 if (!anItem->isEditable())
292 myEdit = anItem->startEditing();
294 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
295 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
296 myEditedItem = anItem;
303 //================================================================
304 // Function : QAD_ListView::resizeEvent
305 // Purpose : called when Data Viewer is resized
306 //================================================================
307 void QAD_ListView::resizeEvent( QResizeEvent * e)
309 QListView::resizeEvent(e);
310 int aW = columnWidth(columns()-1);
311 int aX = header()->sectionPos(columns()-1);
312 if (aW < width() - frameWidth() * 2 - aX - 1)
313 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
317 //================================================================
318 // Function : QAD_ListView::onHeaderSizeChange
319 // Purpose : slot, called when columns sizes are changed
320 //================================================================
321 void QAD_ListView::onHeaderSizeChange(int, int, int)
323 int aW = columnWidth(columns()-1);
324 int aX = header()->sectionPos(columns()-1);
325 if (aW < width() - frameWidth() * 2 - aX - 1)
326 setColumnWidth(columns()-1, width() - frameWidth() * 2 - aX - 1);
329 //================================================================
330 // Function : QAD_ListView::viewportPaintEvent
331 // Purpose : handler for paint event
332 //================================================================
333 void QAD_ListView::viewportPaintEvent(QPaintEvent* e)
335 QListView::viewportPaintEvent(e);
336 if (myEditedItem && myEdit) {
337 computeEditGeometry(myEditedItem, myEdit);
341 //================================================================
342 // Function : QAD_ListView::onEditOk
343 // Purpose : called when user finishes in editing of item
344 //================================================================
345 void QAD_ListView::onEditOk()
350 //================================================================
351 // Function : QAD_ListView::onEditCancel
352 // Purpose : called when user cancels item editing
353 //================================================================
354 void QAD_ListView::onEditCancel()
356 finishEditing(false);
359 //================================================================
360 // Function : QAD_ListView::finishEditing
361 // Purpose : finishes editing of entity
362 //================================================================
363 UpdateType QAD_ListView::finishEditing(bool ok)
365 UpdateType aNeedsUpdate = utCancel;
366 if (myEditedItem && myEdit)
368 disconnect(myEdit, SIGNAL(returnPressed()), this, SLOT(onEditOk()));
369 disconnect(myEdit, SIGNAL(escapePressed()), this, SLOT(onEditCancel()));
370 myEditedItem->setAccepted(true);
372 aNeedsUpdate = myEditedItem->finishEditing(myEdit);
373 if (aNeedsUpdate == utCancel) {
374 // something to do here on Cancel...
377 // something to do here on OK...
380 switch (aNeedsUpdate) {
384 myEditedItem->updateAllLevels();
390 QAD_ListViewItem* aParent = (QAD_ListViewItem*)(myEditedItem->parent());
392 aParent->updateAllLevels();
394 myEditedItem->updateAllLevels();
405 // doing the same as for utUpdateViewer here
406 // descendants can add extra processing
416 // hide <myEdit> widget
424 //================================================================
425 // Function : QAD_ListView::tip
426 // Purpose : gets current tooltip for list view
427 // returns valid rect in success
428 //================================================================
429 QRect QAD_ListView::tip(QPoint aPos,
434 QRect result( -1, -1, -1, -1 );
435 QAD_ListViewItem* aItem = (QAD_ListViewItem*)itemAt( aPos );
437 for (int i = 0; i < columns(); i++) {
438 QRect aItemRect = aItem->itemRect(i);
439 QRect aTextRect = aItem->textRect(i);
440 if ( !aItem->text(i).isEmpty() &&
441 ( aItemRect.width() > header()->sectionSize(i) ||
442 aTextRect.left() < 0 ||
443 aTextRect.top() < 0 ||
444 aTextRect.right() > viewport()->width() ||
445 aTextRect.bottom() > viewport()->height() ) ) {
446 // calculating tip data
447 aText = aItem->tipText();
448 dspRect = aItem->tipRect();
450 if (dspRect.isValid()) {
451 result = QRect(QPoint(0, aItemRect.top()),
452 QSize(viewport()->width(), aItemRect.height()));
460 //////////////////////////////////////////////////////////////////////
461 // QAD_ListViewItem Class Implementation
462 //////////////////////////////////////////////////////////////////////
464 //================================================================
465 // Function : QAD_ListViewItem::QAD_ListViewItem
466 // Purpose : constructor
467 //================================================================
468 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent) :
469 QListViewItem( parent )
474 //================================================================
475 // Function : QAD_ListViewItem::QAD_ListViewItem
476 // Purpose : constructor
477 //================================================================
478 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
479 QAD_ListViewItem* after) :
480 QListViewItem( parent, after )
485 //================================================================
486 // Function : QAD_ListViewItem::QAD_ListViewItem
487 // Purpose : constructor
488 //================================================================
489 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
490 const QString theName,
491 const bool theEditable) :
492 QListViewItem(parent, theName)
495 setEditable(theEditable);
498 //================================================================
499 // Function : QAD_ListViewItem::QAD_ListViewItem
500 // Purpose : constructor
501 //================================================================
502 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
503 const QString theName,
504 const QString theValue,
505 const bool theEditable) :
506 QListViewItem(parent, theName, theValue)
509 setEditable(theEditable);
512 //================================================================
513 // Function : QAD_ListViewItem::QAD_ListViewItem
514 // Purpose : constructor
515 //================================================================
516 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
517 const QString theName,
518 const bool theEditable) :
519 QListViewItem(parent, theName)
522 setEditable(theEditable);
525 //================================================================
526 // Function : QAD_ListViewItem::QAD_ListViewItem
527 // Purpose : constructor
528 //================================================================
529 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
530 QAD_ListViewItem* after,
531 const QString theName,
532 const bool theEditable) :
533 QListViewItem(parent, after, theName)
536 setEditable(theEditable);
539 //================================================================
540 // Function : QAD_ListViewItem::QAD_ListViewItem
541 // Purpose : constructor
542 //================================================================
543 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
544 QAD_ListViewItem* after,
545 const QString theName,
546 const bool theEditable) :
547 QListViewItem(parent, after, theName)
550 setEditable(theEditable);
554 //================================================================
555 // Function : QAD_ListViewItem::QAD_ListViewItem
556 // Purpose : constructor
557 //================================================================
558 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
559 const QString theName,
560 const QString theValue,
561 const bool theEditable) :
562 QListViewItem(parent, theName, theValue)
565 setEditable(theEditable);
569 //================================================================
570 // Function : QAD_ListViewItem::QAD_ListViewItem
571 // Purpose : constructor
572 //================================================================
573 QAD_ListViewItem::QAD_ListViewItem(QAD_ListViewItem* parent,
574 QAD_ListViewItem* after,
575 const QString theName,
576 const QString theValue,
577 const bool theEditable) :
578 QListViewItem(parent, after, theName, theValue)
581 setEditable(theEditable);
584 //================================================================
585 // Function : QAD_ListViewItem::QAD_ListViewItem
586 // Purpose : constructor
587 //================================================================
588 QAD_ListViewItem::QAD_ListViewItem(QAD_ListView* parent,
589 QAD_ListViewItem* after,
590 const QString theName,
591 const QString theValue,
592 const bool theEditable) :
593 QListViewItem(parent, after, theName, theValue)
596 setEditable(theEditable);
599 //================================================================
600 // Function : QAD_ListViewItem::~QAD_ListViewItem
601 // Purpose : destructor
602 //================================================================
603 QAD_ListViewItem::~QAD_ListViewItem()
607 //================================================================
608 // Function : QAD_ListViewItem::init
609 // Purpose : initialization
610 //================================================================
611 void QAD_ListViewItem::init()
615 myEditingType = (int)QAD_EntityEdit::etLineEdit;
616 myValueType = (int)QAD_EntityEdit::vtString;
621 //================================================================
622 // Function : QAD_ListViewItem::getName
623 // Purpose : as default returns text in the first column
624 //================================================================
625 QString QAD_ListViewItem::getName() const
627 return ( listView()->columns() > 0 ) ? text(0) : QString("");
630 //================================================================
631 // Function : QAD_ListViewItem::setName
632 // Purpose : as default sets text in the first column
633 //================================================================
634 UpdateType QAD_ListViewItem::setName(const QString theName)
636 UpdateType aNeedsUpdate = utCancel;
637 if (listView()->columns() > 0) {
639 aNeedsUpdate = utNone;
644 //================================================================
645 // Function : QAD_ListViewItem::getValue
646 // Purpose : as default returns text in the second column
647 //================================================================
648 QString QAD_ListViewItem::getValue() const
650 return ( listView()->columns() > 1 ) ? text(1) : QString("");
653 //================================================================
654 // Function : QAD_ListViewItem::setValue
655 // Purpose : as default sets text in the second column
656 //================================================================
657 UpdateType QAD_ListViewItem::setValue(const QString theValue)
659 UpdateType aNeedsUpdate = utCancel;
660 if (listView()->columns() > 1) {
661 setText(1, theValue);
662 aNeedsUpdate = utNone;
667 //================================================================
668 // Function : QAD_ListViewItem::fullName
669 // Purpose : returns full path to the entity from the root
670 //================================================================
671 QString QAD_ListViewItem::fullName()
673 QString aFullName = getName();
674 QAD_ListViewItem* aParent = (QAD_ListViewItem*)parent();
675 while(aParent != NULL) {
676 aFullName = aParent->getName() + QString(".") + aFullName;
677 aParent = (QAD_ListViewItem*)(aParent->parent());
682 //================================================================
683 // Function : QAD_ListViewItem::openAllLevels
684 // Purpose : expands all entities beginning from this level
685 //================================================================
686 void QAD_ListViewItem::openAllLevels()
689 QAD_ListViewItem* aChild = (QAD_ListViewItem*)firstChild();
691 aChild->openAllLevels();
692 aChild = (QAD_ListViewItem*)(aChild->nextSibling());
696 //================================================================
697 // Function : QAD_ListViewItem::updateAllLevels
698 // Purpose : update all entites beginning from this level
699 //================================================================
700 void QAD_ListViewItem::updateAllLevels()
702 QAD_ListViewItem* aChild = (QAD_ListViewItem*)firstChild();
704 aChild->updateAllLevels();
705 aChild = (QAD_ListViewItem*)(aChild->nextSibling());
709 //================================================================
710 // Function : QAD_EditBox::isEditable
711 // Purpose : return true if entity is editable
712 //================================================================
713 bool QAD_ListViewItem::isEditable() const
718 //================================================================
719 // Function : QAD_ListViewItem::setEditable
720 // Purpose : sets editable flag fo the entity
721 //================================================================
722 void QAD_ListViewItem::setEditable(bool theEditable)
724 myEditable = theEditable;
727 //================================================================
728 // Function : QAD_ListViewItem::isAccepted
729 // Purpose : returns true if entitiy is accepted after editing
730 //================================================================
731 bool QAD_ListViewItem::isAccepted() const
736 //================================================================
737 // Function : QAD_ListViewItem::setAccepted
738 // Purpose : set entitiy accepted or not after editing
739 //================================================================
740 void QAD_ListViewItem::setAccepted(bool theAccepted)
742 myAccepted = theAccepted;
745 //================================================================
746 // Function : QAD_ListViewItem::getEditingType
747 // Purpose : returns type of edit control (0 - edit box, 1 - combo box,
748 // 2 - editable combo box), default is edit box
749 //================================================================
750 int QAD_ListViewItem::getEditingType()
752 return myEditingType;
755 //================================================================
756 // Function : QAD_ListViewItem::setEditingType
757 // Purpose : sets type of edit control (0 - edit box, 1 - combo box,
758 // 2 - editable combo box), negative value means none
759 //================================================================
760 void QAD_ListViewItem::setEditingType(const int type)
762 myEditingType = type;
765 //================================================================
766 // Function : QAD_ListViewItem::getEditedColumn
767 // Purpose : returns edited column, default is last column
768 // negative value means there are no editable columns
769 //================================================================
770 int QAD_ListViewItem::getEditedColumn()
772 return listView()->columns()-1;
775 //================================================================
776 // Function : QAD_ListViewItem::getValueType
777 // Purpose : returns type of edited value (string, int, double)
779 //================================================================
780 int QAD_ListViewItem::getValueType()
785 //================================================================
786 // Function : QAD_ListViewItem::setValueType
787 // Purpose : sets type of edited value
788 //================================================================
789 void QAD_ListViewItem::setValueType(const int valueType)
791 myValueType = valueType;
794 //================================================================
795 // Function : QAD_ListViewItem::getUserType
796 // Purpose : sets type of edited value
797 //================================================================
798 int QAD_ListViewItem::getUserType()
803 //================================================================
804 // Function : QAD_ListViewItem::setUserType
805 // Purpose : sets type of edited value
806 //================================================================
807 void QAD_ListViewItem::setUserType(const int userType)
809 myUserType = userType;
812 //================================================================
813 // Function : QAD_ListViewItem::getButtons
814 // Purpose : returns buttons for editing widget (Apply (V), Cancel (X))
815 // default is both buttons
816 //================================================================
817 int QAD_ListViewItem::getButtons()
822 //================================================================
823 // Function : QAD_ListViewItem::getButtons
824 // Purpose : sets buttons for editing widget (Apply (V), Cancel (X))
825 //================================================================
826 void QAD_ListViewItem::setButtons(const int buttons)
831 //================================================================
832 // Function : QAD_ListViewItem::startEditing
833 // Purpose : creates control for editing and fills it with values
834 //================================================================
835 QAD_EntityEdit* QAD_ListViewItem::startEditing()
837 QAD_EntityEdit* aWidget = 0;
838 QListView* aListView = listView();
842 int anEditType = getEditingType();
843 int aValueType = getValueType();
844 int aButtons = getButtons();
845 int anEditColumn = getEditedColumn();
846 if (anEditColumn < 0 || anEditType < 0)
848 aWidget = new QAD_EntityEdit(aListView->viewport(),
851 aButtons & QAD_EntityEdit::btApply,
852 aButtons & QAD_EntityEdit::btCancel);
853 computeEditGeometry(this, aWidget);
855 fillWidgetWithValues(aWidget);
860 //================================================================
861 // Function : QAD_ListViewItem::fillWidgetWithValues
862 // Purpose : fills widget with initial values (list or single value)
863 //================================================================
864 void QAD_ListViewItem::fillWidgetWithValues(QAD_EntityEdit* theWidget)
866 int anEditColumn = getEditedColumn();
867 if (theWidget && anEditColumn >= 0 && !text(anEditColumn).isEmpty())
868 theWidget->insertItem(text(anEditColumn), true);
871 //================================================================
872 // Function : QAD_ListViewItem::finishEditing
873 // Purpose : finishes editing of entity
874 //================================================================
875 UpdateType QAD_ListViewItem::finishEditing(QAD_EntityEdit* theWidget)
877 UpdateType aNeedsUpdate = utCancel;
880 int anEditColumn = getEditedColumn();
881 switch (anEditColumn) {
883 aNeedsUpdate = setName(theWidget->getText());
886 aNeedsUpdate = setValue(theWidget->getText());
894 MESSAGE( "System error has been caught - QAD_ListViewItem::finishEditing" )
899 //================================================================
900 // Function : QAD_ListViewItem::tipRect
901 // Purpose : calculates rectangle which should contain item's tip
902 //================================================================
903 QRect QAD_ListViewItem::tipRect()
905 QRect aRect = QRect(-1, -1, -1, -1);
906 QRect aItemRect = listView()->itemRect(this);
907 if ( !aItemRect.isValid() )
910 QString aTip = tipText();
911 if (!aTip.isEmpty()) {
912 QRect aRect0 = textRect(0);
913 QFont aFont(listView()->font());
914 QFontMetrics fm(aFont);
915 int iw = fm.width(aTip);
916 aRect = QRect(QPoint(aRect0.x() < 0 ? 0 : aRect0.x(),
924 //================================================================
925 // Function : QAD_ListViewItem::tipText
926 // Purpose : returns text for tooltip
927 //================================================================
928 QString QAD_ListViewItem::tipText()
930 QString aText = getName();
931 if (!getValue().isEmpty())
932 aText += QString(" : ") + getValue();
936 //================================================================
937 // Function : QAD_ListViewItem::textRect
938 // Purpose : calculates rect of item text in viewport coordinates
939 //================================================================
940 QRect QAD_ListViewItem::textRect(const int column) const
942 QRect aItemRect = listView()->itemRect( this );
943 if ( !aItemRect.isValid() )
946 QFont aFont(listView()->font());
947 QFontMetrics fm(aFont);
949 int decorWidth = ( listView()->rootIsDecorated() ) ?
950 ( listView()->treeStepSize() * (depth() + 1) ) :
951 ( listView()->treeStepSize() * depth() );
952 int pixmapWidth = ( pixmap(column) ) ?
953 pixmap(column)->width() + listView()->itemMargin() * 2 :
954 listView()->itemMargin();
956 for (int i = 0; i < column; i++)
957 prevWidth += listView()->header()->sectionSize(i);
960 ((column == 0) ? decorWidth : 0);
961 int iy = aItemRect.y();
962 int iw = fm.width(text(column));
963 int ih = aItemRect.height();
964 if (pixmap(column)) {
965 iy += listView()->itemMargin();
966 ih -= listView()->itemMargin() * 2;
968 ix -= listView()->contentsX();
970 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
974 //================================================================
975 // Function : QAD_ListViewItem::itemRect
976 // Purpose : calculates rect of item data in viewport coordinates
977 //================================================================
978 QRect QAD_ListViewItem::itemRect(const int column) const
980 QRect aItemRect = listView()->itemRect( this );
981 if ( !aItemRect.isValid() )
984 QFont aFont(listView()->font());
985 QFontMetrics fm(aFont);
987 int decorWidth = ( listView()->rootIsDecorated() ) ?
988 ( listView()->treeStepSize() * (depth() + 1) ) :
989 ( listView()->treeStepSize() * depth() );
990 int pixmapWidth = ( pixmap(column) ) ?
991 pixmap(column)->width() + listView()->itemMargin() * 2 :
994 for (int i = 0; i < column; i++)
995 prevWidth += listView()->header()->sectionSize(i);
997 int iy = aItemRect.y();
998 int iw = pixmapWidth +
999 listView()->itemMargin() * 2 +
1000 ((column == 0) ? decorWidth : 0) +
1001 fm.width(text(column));
1002 int ih = aItemRect.height();
1003 ix -= listView()->contentsX();
1005 QRect theResult(QPoint(ix, iy), QSize(iw, ih));
1009 //////////////////////////////////////////////////////////////////////
1010 // QAD_EditBox class implementation
1011 //////////////////////////////////////////////////////////////////////
1013 //================================================================
1014 // Function : QAD_EditBox::QAD_EditBox
1015 // Purpose : constructor
1016 //================================================================
1017 QAD_EditBox::QAD_EditBox(QWidget* parent) :
1022 //================================================================
1023 // Function : QAD_EditBox::keyPressEvent
1024 // Purpose : event filter for key pressing
1025 //================================================================
1026 void QAD_EditBox::keyPressEvent( QKeyEvent *e )
1028 if ( e->key() == Key_Escape )
1029 emit escapePressed();
1031 QLineEdit::keyPressEvent( e );
1035 //////////////////////////////////////////////////////////////////////
1036 // QAD_ComboBox class implementation
1037 //////////////////////////////////////////////////////////////////////
1039 //================================================================
1040 // Function : QAD_ComboBox::QAD_ComboBox
1041 // Purpose : constructor
1042 //================================================================
1043 QAD_ComboBox::QAD_ComboBox(bool rw, QWidget* parent, const char* name) :
1044 QComboBox(rw, parent, name)
1048 //================================================================
1049 // Function : QAD_ComboBox::findItem
1050 // Purpose : searches item in list and returns its index
1051 //================================================================
1052 int QAD_ComboBox::findItem(const QString theText)
1054 for (int i = 0; i < count(); i++)
1055 if (text(i) == theText)
1060 //================================================================
1061 // Function : QAD_ComboBox::insertItem
1062 // Purpose : adds item in combo box
1063 //================================================================
1064 void QAD_ComboBox::insertItem(const QString& theValue,
1067 if (duplicatesEnabled() || findItem(theValue) < 0)
1068 QComboBox::insertItem(theValue, theIndex);
1071 //================================================================
1072 // Function : QAD_ComboBox::insertList
1073 // Purpose : adds list of items in combo box
1074 //================================================================
1075 void QAD_ComboBox::insertList(const QStringList& theList)
1077 for (unsigned i = 0; i < theList.count(); i++)
1078 insertItem(theList[i]);
1081 //================================================================
1082 // Function : QAD_ComboBox::insertItem
1083 // Purpose : adds item in combo box
1084 //================================================================
1085 void QAD_ComboBox::insertItem(const int theValue)
1089 for (int i = 0; i < count(); i++) {
1090 aNum = text(i).toInt(&bOk);
1092 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1093 insertItem(QString::number(theValue), i);
1098 insertItem(QString::number(theValue));
1101 //================================================================
1102 // Function : QAD_ComboBox::insertList
1103 // Purpose : adds list of items in combo box
1104 //================================================================
1105 void QAD_ComboBox::insertList(const TColStd_ListOfInteger& theList)
1107 for (TColStd_ListIteratorOfListOfInteger aIter(theList); aIter.More(); aIter.Next())
1108 insertItem(aIter.Value());
1111 //================================================================
1112 // Function : QAD_ComboBox::insertItem
1113 // Purpose : adds item in combo box
1114 //================================================================
1115 void QAD_ComboBox::insertItem(const double theValue)
1119 for (int i = 0; i < count(); i++) {
1120 aNum = text(i).toDouble(&bOk);
1122 if (aNum > theValue || (aNum == theValue && duplicatesEnabled())) {
1123 insertItem(QString::number(theValue), i);
1128 insertItem(QString::number(theValue));
1131 //================================================================
1132 // Function : QAD_ComboBox::insertList
1133 // Purpose : adds list of items in combo box
1134 //================================================================
1135 void QAD_ComboBox::insertList(const TColStd_ListOfReal& theList)
1137 for (TColStd_ListIteratorOfListOfReal aIter(theList); aIter.More(); aIter.Next())
1138 insertItem(aIter.Value());
1141 //////////////////////////////////////////////////////////////////////
1142 // QAD_EntityEdit class implementation
1143 //////////////////////////////////////////////////////////////////////
1145 #include <qlayout.h>
1147 #define MIN_COMBO_WIDTH 1
1148 #define MIN_EDIT_WIDTH 1
1150 //================================================================
1151 // Function : QAD_EntityEdit::QAD_EntityEdit
1152 // Purpose : constructor
1153 //================================================================
1154 QAD_EntityEdit::QAD_EntityEdit(QWidget* parent,
1165 QHBoxLayout* aTopLayout = new QHBoxLayout(this);
1166 aTopLayout->setAlignment( Qt::AlignTop );
1167 aTopLayout->setSpacing( 0 );
1168 aTopLayout->setMargin( 1 );
1169 if (controlType != etLineEdit &&
1170 controlType != etComboBox &&
1171 controlType != etComboEdit)
1172 controlType = etLineEdit;
1173 if (controlType == etComboBox || controlType == etComboEdit) {
1174 // this is an editable combo box
1175 myCombo = new QAD_ComboBox(controlType == etComboEdit, this);
1176 myCombo->setMinimumSize(MIN_COMBO_WIDTH, 0);
1177 myCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1178 QSizePolicy::Fixed));
1180 myCombo->setInsertionPolicy(QComboBox::NoInsertion);
1181 // no duplicates enabled by default
1182 myCombo->setDuplicatesEnabled(false);
1183 aTopLayout->addWidget(myCombo);
1185 connect(myCombo, SIGNAL(activated(const QString&)), this, SLOT(onComboActivated(const QString&)));
1186 connect(myCombo, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1189 // and this is an edit box
1190 myEdit = new QAD_EditBox(this);
1191 myEdit->setMinimumSize(MIN_EDIT_WIDTH, 0);
1192 myEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
1193 QSizePolicy::Fixed));
1194 aTopLayout->addWidget(myEdit);
1195 connect(myEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
1196 connect(myEdit, SIGNAL(returnPressed()), this, SLOT(onApply()));
1197 connect(myEdit, SIGNAL(escapePressed()), this, SLOT(onCancel()));
1199 if (valueType != vtString &&
1200 valueType != vtInteger &&
1201 valueType != vtDouble)
1202 valueType = vtString;
1203 if (valueType == vtInteger)
1204 setValidator(new QIntValidator(this));
1205 else if (valueType == vtDouble)
1206 setValidator(new QDoubleValidator(this));
1209 myApplyBtn = new QToolButton(this);
1210 QPixmap anIcon = QAD_Desktop::getResourceManager()->loadPixmap(
1211 "QAD",tr("ICON_APPLY_BTN"));
1212 myApplyBtn->setPixmap(anIcon);
1213 myApplyBtn->setEnabled(false);
1214 myApplyBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1215 myApplyBtn->setMinimumSize(16, 16);
1216 myApplyBtn->setMaximumSize(16, 20);
1217 aTopLayout->addWidget(myApplyBtn);
1218 connect(myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
1221 // Cancel button (X)
1222 myCancelBtn = new QToolButton(this);
1223 QPixmap anIcon = QAD_Desktop::getResourceManager()->loadPixmap(
1224 "QAD",tr("ICON_CANCEL_BTN"));
1225 myCancelBtn->setPixmap(anIcon);
1226 myCancelBtn->setEnabled(false);
1227 myCancelBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
1228 myCancelBtn->setMinimumSize(16, 16);
1229 myCancelBtn->setMaximumSize(16, 20);
1230 aTopLayout->addWidget(myCancelBtn);
1231 connect(myCancelBtn, SIGNAL(clicked()), this, SLOT(onCancel()));
1235 //================================================================
1236 // Function : QAD_EntityEdit::~QAD_EntityEdit
1237 // Purpose : destructor
1238 //================================================================
1239 QAD_EntityEdit::~QAD_EntityEdit()
1243 //================================================================
1244 // Function : QAD_EntityEdit::clear
1245 // Purpose : clears edit/combo box
1246 //================================================================
1247 void QAD_EntityEdit::clear()
1255 //================================================================
1256 // Function : QAD_EntityEdit::getText
1257 // Purpose : returns current text in edit box or combo box
1258 //================================================================
1259 QString QAD_EntityEdit::getText()
1262 return myEdit->text();
1264 return myCombo->currentText();
1269 //================================================================
1270 // Function : QAD_EntityEdit::setText
1271 // Purpose : sets text
1272 //================================================================
1273 void QAD_EntityEdit::setText(const QString& theText)
1277 myEdit->setText(theText);
1279 int aFound = myCombo->findItem(theText);
1281 myCombo->setCurrentItem(aFound);
1282 onTextChanged(theText);
1287 //================================================================
1288 // Function : QAD_EntityEdit::insertItem
1289 // Purpose : adds item in combo box,
1290 // sets it current if theSetCurrent is true
1291 //================================================================
1292 void QAD_EntityEdit::insertItem(const QString& theValue,
1298 if (theOrder == atTop)
1300 else if (theOrder == atBeforeCurrent && myCombo->count() > 0)
1301 aIndexAt = myCombo->currentItem();
1302 else if (theOrder == atAfterCurrent &&
1303 myCombo->count() > 0 &&
1304 myCombo->currentItem() < myCombo->count()-1)
1305 aIndexAt = myCombo->currentItem() + 1;
1306 myCombo->insertItem(theValue, aIndexAt);
1312 //================================================================
1313 // Function : QAD_EntityEdit::insertList
1314 // Purpose : adds items in combo box,
1315 // sets item theCurrent as current
1316 //================================================================
1317 void QAD_EntityEdit::insertList(const QStringList& theList,
1318 const int theCurrent)
1321 myCombo->insertList(theList);
1322 if (theCurrent >= 0 && theCurrent < (int)theList.count())
1323 setText(theList[theCurrent]);
1326 //================================================================
1327 // Function : QAD_EntityEdit::insertItem
1328 // Purpose : adds item in combo box,
1329 // sets it current if theSetCurrent is true
1330 //================================================================
1331 void QAD_EntityEdit::insertItem(const int theValue,
1335 myCombo->insertItem(theValue);
1338 setText(QString::number(theValue));
1341 //================================================================
1342 // Function : QAD_EntityEdit::insertList
1343 // Purpose : adds items in combo box,
1344 // sets item theCurrent as current
1345 //================================================================
1346 void QAD_EntityEdit::insertList(const TColStd_ListOfInteger& theList,
1347 const int theCurrent)
1350 myCombo->insertList(theList);
1352 TColStd_ListIteratorOfListOfInteger aIter(theList);
1353 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1354 if (theCurrent == i) {
1355 setText(QString::number(aIter.Value()));
1361 //================================================================
1362 // Function : QAD_EntityEdit::insertItem
1363 // Purpose : adds item in combo box,
1364 // sets it current if theSetCurrent is true
1365 //================================================================
1366 void QAD_EntityEdit::insertItem(const double theValue,
1370 myCombo->insertItem(theValue);
1373 setText(QString::number(theValue));
1376 //================================================================
1377 // Function : QAD_EntityEdit::insertList
1378 // Purpose : adds items in combo box,
1379 // sets item theCurrent as current
1380 //================================================================
1381 void QAD_EntityEdit::insertList(const TColStd_ListOfReal& theList,
1382 const int theCurrent)
1385 myCombo->insertList(theList);
1387 TColStd_ListIteratorOfListOfReal aIter(theList);
1388 for (unsigned i = 0; aIter.More(); aIter.Next(), i++) {
1389 if (theCurrent == i) {
1390 setText(QString::number(aIter.Value()));
1396 //================================================================
1397 // Function : QAD_EntityEdit::getControl
1398 // Purpose : gets actual widget
1399 //================================================================
1400 QWidget* QAD_EntityEdit::getControl()
1410 //================================================================
1411 // Function : QAD_EntityEdit::setFocus
1412 // Purpose : redirect focus to corresponding widget
1413 //================================================================
1414 void QAD_EntityEdit::setFocus()
1418 //myEdit->selectAll();
1420 else if (myCombo && myCombo->editable()) {
1421 myCombo->setFocus();
1422 //myCombo->lineEdit()->selectAll();
1426 //================================================================
1427 // Function : QAD_EntityEdit::setValidator
1428 // Purpose : sets validator for the control
1429 //================================================================
1430 void QAD_EntityEdit::setValidator(const QValidator* theValidator)
1433 myEdit->setValidator(theValidator);
1435 myCombo->setValidator(theValidator);
1438 //================================================================
1439 // Function : QAD_EntityEdit::keyPressEvent
1440 // Purpose : event filter for KeyPress event
1441 //================================================================
1442 void QAD_EntityEdit::keyPressEvent( QKeyEvent * e)
1444 if ( (e->key() == Key_Enter ||
1445 e->key() == Key_Return ) )
1447 else if (e->key() == Key_Escape)
1451 //================================================================
1452 // Function : QAD_EntityEdit::onComboActivated
1453 // Purpose : called when item activated in combo box
1454 //================================================================
1455 void QAD_EntityEdit::onComboActivated(const QString& theText)
1457 onTextChanged(theText);
1460 //================================================================
1461 // Function : QAD_EntityEdit::onTextChanged
1462 // Purpose : slot, called when text changed in line edit
1463 //================================================================
1464 void QAD_EntityEdit::onTextChanged(const QString& theText)
1467 myApplyBtn->setEnabled(!(theText == myString));
1469 myCancelBtn->setEnabled(!(theText == myString));
1472 //================================================================
1473 // Function : QAD_EntityEdit::onCancel
1474 // Purpose : slot, called when user presses Cancel button
1475 //================================================================
1476 void QAD_EntityEdit::onCancel()
1480 myApplyBtn->setEnabled(false);
1482 myCancelBtn->setEnabled(false);
1483 emit escapePressed();
1486 //================================================================
1487 // Function : QAD_EntityEdit::onApply
1488 // Purpose : slot, called when user presses Apply button
1489 //================================================================
1490 void QAD_EntityEdit::onApply()
1492 myString = getText();
1494 myApplyBtn->setEnabled(false);
1496 myCancelBtn->setEnabled(false);
1497 emit returnPressed();
1500 //================================================================
1501 // Function : QAD_EntityEdit::showButtons
1502 // Purpose : shows/hides buttons
1503 //================================================================
1504 void QAD_EntityEdit::showButtons(bool show)
1507 show ? myApplyBtn->show() : myApplyBtn->hide();
1509 show ? myCancelBtn->show() : myCancelBtn->hide();
1512 //================================================================
1513 // Function : QAD_EntityEdit::setDuplicatesEnabled
1514 // Purpose : enables/disables data duplication (for combo box)
1515 //================================================================
1516 void QAD_EntityEdit::setDuplicatesEnabled(bool enabled)
1519 myCombo->setDuplicatesEnabled(enabled);