File: CPropertiesModel.cpp

package info (click to toggle)
unixodbc 2.2.14p2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 14,628 kB
  • ctags: 12,533
  • sloc: ansic: 104,243; cpp: 38,571; sh: 15,958; makefile: 2,727; sql: 1
file content (196 lines) | stat: -rw-r--r-- 5,997 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*!
 * \file
 *
 * \author  Peter Harvey <pharvey@peterharvey.org>
 * \author  \sa AUTHORS file
 * \version 2
 * \date    2007
 * \license Copyright unixODBC Project 2007-2008, LGPL
 */
#include <QtGui>

#include "CPropertiesModel.h"

#include "CPropertiesDelegate.h"

/*! 
 * \brief   Construct a properties model.
 * 
 * \param   pobjectParent
 * \param   hFirstProperty
 */
CPropertiesModel::CPropertiesModel( QObject *pobjectParent, HODBCINSTPROPERTY pFirstProperty )
    : QAbstractTableModel( pobjectParent )
{
    /* create an index on our linked-list to simplify, and speed up, access */
    for ( HODBCINSTPROPERTY pProperty = pFirstProperty; pProperty != NULL; pProperty = pProperty->pNext )
    {
        vectorProperties.append( pProperty );
    }
}

CPropertiesModel::~CPropertiesModel()
{
}

/*! 
 * \brief   Return the number of rows needed to view the properties.
 * 
 *          
 * \return  int Output. The number of properties.
 */
int CPropertiesModel::rowCount( const QModelIndex & ) const
{
    return vectorProperties.size();
}

/*! 
 * \brief   Return the number of columns needed to view the properties.
 * 
 *          We always return 2. We have 1 column with the property name and 1 column with the
 *          value - the latter us typically, but not always, editable.
 * 
 * \return  int Output. Always 2.
 */
int CPropertiesModel::columnCount( const QModelIndex & ) const
{
    return 2;
}

/*! 
 * \brief   Provide cell data to the view and any delegate we may have in play
 *          for the cell.
 *
 *          We expect that the 2nd column will have CPropertiesDelegate installed on it. So for
 *          the 2nd column we may return the value text or HODBCINSTPROPERTY.
 *
 * \param   index
 * \param   nRole   Input. Qt::ItemDataRole. This is provided to request a specific variant of the
 *                  data associated with the cell.
 * 
 * \return  QVariant
 */
QVariant CPropertiesModel::data( const QModelIndex &index, int nRole ) const
{
    /* \todo are these sanity checks really needed? */
    if ( !index.isValid() )
        return QVariant();
    if ( index.row() >= vectorProperties.size() )
         return QVariant();

    /* which column ? */
    if ( index.column() == 0 )
    {
        switch ( nRole )
        {
            case Qt::DisplayRole:   /* called by QTableView to display the value text   */
                return vectorProperties.at( index.row() )->szName;
            case Qt::EditRole:      /* the 1st column should not be editable but...     */
                return vectorProperties.at( index.row() )->szName;
            case Qt::ToolTipRole:
            case Qt::StatusTipRole:
            case Qt::WhatsThisRole:
                return vectorProperties.at( index.row() )->pszHelp;
            default:
                break;
        }
    }
    else
    {
        switch ( nRole )
        {
            case Qt::DisplayRole:   /* called by QTableView to display the value text without editor                    */
                return vectorProperties.at( index.row() )->szValue;
            case Qt::EditRole:      /* called by CPropertiesDelegate:: createEditor & setEditorData to load cell editor */
                return QVariant::fromValue( vectorProperties.at( index.row() ) );
            case Qt::ToolTipRole:
            case Qt::StatusTipRole:
            case Qt::WhatsThisRole:
                return vectorProperties.at( index.row() )->pszHelp;
            default:
                break;
        }
    }

    return QVariant();
}

/*! 
 * \brief   Set data.
 * 
 *          The view (possibly via a delegate) will ask to set the cell value which will result
 *          in a call to this method.
 *
 * \param   index           Input. Index into the cell.
 * \param   variantValue    Input. The only column we deal with here is the 2nd column (the value) and 
 *                          we expect to get that as a QString. This should come from 
 *                          \sa CPropertiesDelegate::setModelData.
 * \param   nRole           Input. Qt::ItemDataRole. This is provided to request setting a specific variant 
 *                          of the data associated with the cell.
 * 
 * \return bool
 */
bool CPropertiesModel::setData( const QModelIndex &index, const QVariant &variantValue, int nRole )
{
    if ( !index.isValid() )
        return false;

    if ( index.column() == 0 )
        return false;

    if ( nRole == Qt::EditRole )
    {
        strcpy( vectorProperties.at( index.row() )->szValue, variantValue.toString().toAscii().constData() );
        emit dataChanged( index, index );
        return true;
    }

    return false;
}

/*! 
 * \brief   The delegate will check this to determine what it can do with a cell.
 * 
 * \param   index   Input. Cell index.
 * 
 * \return  Qt::ItemFlags
 */
Qt::ItemFlags CPropertiesModel::flags( const QModelIndex &index ) const
{
    if ( !index.isValid() )
        return Qt::ItemIsEnabled; /* for lack of a better choice :/ */

    if ( index.column() == 1 )
        return ( QAbstractItemModel::flags( index ) | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );

    return Qt::ItemIsEnabled; /* for lack of a better choice :/ */
}

/*! 
 * \brief   QTableView (or possibly a delegate) will get column headers here.
 * 
 * \param   nSection        Input. Column/row.
 * \param   nOrientation    Input. Qt::Orientation.
 * \param   nRole           Input. Qt::ItemDataRole. This is provided to request a specific variant 
 *                          of the data associated with the cell.
 * 
 * \return QVariant
 */
QVariant CPropertiesModel::headerData( int nSection, Qt::Orientation nOrientation, int nRole ) const
{
    if ( nRole != Qt::DisplayRole )
         return QVariant();
    if ( nOrientation != Qt::Horizontal )
        return QVariant();

    if ( nSection == 0 )
        return QString( tr( "Name" ) );

    if ( nSection == 1 )
        return QString( tr( "Value" ) );

    return QVariant();
}