0029571: Samples: build qt samples together with OCCT
[occt.git] / samples / tools / TInspectorEXE / src / TInspectorEXE_OpenFileViewModel.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <TInspectorEXE_OpenFileViewModel.hxx>
17
18 #include <QApplication>
19 #include <QFileInfo>
20 #include <QIcon>
21 #include <QPainter>
22
23 const int ICON_SIZE = 40;
24
25 // =======================================================================
26 // function : paint
27 // purpose :
28 // =======================================================================
29 void TInspectorEXE_OpenFileItemDelegate::paint (QPainter* thePainter, const QStyleOptionViewItem& theOption,
30                                                 const QModelIndex& theIndex) const
31 {
32   // highlight cell
33   if (theOption.state & QStyle::State_MouseOver)
34     thePainter->fillRect (theOption.rect, myColor);
35
36   // action icon for all indices before the last one
37   QIcon anIcon (":/icons/folder_import.png");
38   QSize anIconSize (ICON_SIZE, ICON_SIZE);
39   int aWidth = theOption.rect.width();
40   int aCenter = aWidth / 2.;
41   int aHalf = anIconSize.width() / 2.;
42   int aMargin = qApp->style()->pixelMetric (QStyle::PM_HeaderMargin);
43   thePainter->drawPixmap (QRect (theOption.rect.left() + (aCenter - aHalf),
44                           theOption.rect.top() + aMargin,
45                           anIconSize.width(),
46                           anIconSize.height()),
47                           anIcon.pixmap(anIconSize.width(), anIconSize.height()));
48   // default paint
49   QItemDelegate::paint (thePainter, theOption, theIndex);
50 }
51
52 // =======================================================================
53 // function : Init
54 // purpose :
55 // =======================================================================
56 void TInspectorEXE_OpenFileViewModel::Init (const QStringList& theValues)
57 {
58   myValues = theValues;
59 }
60
61 // =======================================================================
62 // function : data
63 // purpose :
64 // =======================================================================
65 QVariant TInspectorEXE_OpenFileViewModel::data (const QModelIndex& theIndex, int theRole) const
66 {
67   switch (theRole)
68   {
69     case Qt::DisplayRole: return QFileInfo (myValues[theIndex.column()]).fileName();
70     case Qt::ToolTipRole: return myValues[theIndex.column()];
71     case Qt::TextAlignmentRole: return QVariant (Qt::AlignBottom | Qt::AlignHCenter);
72     default: break;
73   }
74   return QVariant();
75 }