File: CPropertiesDelegate.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 (173 lines) | stat: -rw-r--r-- 5,973 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
/*!
 * \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 "CPropertiesDelegate.h"
#include "CPropertiesModel.h"
#include "CFileSelector.h"

CPropertiesDelegate::CPropertiesDelegate( QObject *pobjectParent )
    : QItemDelegate( pobjectParent )
{
}

/* we could just load the value now and save all of the subsequent logic to do so - but we follow the Qt encouraged flow of things and defer to setEditorData */
QWidget *CPropertiesDelegate::createEditor( QWidget *pwidgetParent, const QStyleOptionViewItem &styleoptionviewitem, const QModelIndex &modelindex ) const
{
    if ( modelindex.column() != 1 )
        return 0;

    HODBCINSTPROPERTY pProperty = modelindex.model()->data( modelindex, Qt::EditRole ).value<HODBCINSTPROPERTY>();

    switch ( pProperty->nPromptType )
    {
        case ODBCINST_PROMPTTYPE_LABEL:
            {
                QLabel *pLabel = new QLabel( pwidgetParent );
                return pLabel;
            }
            break;

        case ODBCINST_PROMPTTYPE_LISTBOX:
            {
                QComboBox *pComboBox = new QComboBox( pwidgetParent );
                for ( int n = 0; (pProperty->aPromptData)[n]; n++ )
                {
                    pComboBox->insertItem( 0, (pProperty->aPromptData)[n] );
                }
                return pComboBox;
            }
            break;

        case ODBCINST_PROMPTTYPE_COMBOBOX:
            {
                QComboBox *pComboBox = new QComboBox( pwidgetParent );
                for ( int n = 0; (pProperty->aPromptData)[n]; n++ )
                {
                    pComboBox->insertItem( 0, (pProperty->aPromptData)[n] );
                }
                pComboBox->setEditable( true );
                return pComboBox;
            }
            break;

        case ODBCINST_PROMPTTYPE_FILENAME:
            {
                CFileSelector *pFileSelector = new CFileSelector( CFileSelector::Driver, QString::null, true, true, pwidgetParent );
                return pFileSelector;
            }
            break;

        case ODBCINST_PROMPTTYPE_HIDDEN:
            break;

        default: // PROMPTTYPE_TEXTEDIT and PROMPTTYPE_TEXTEDIT_PASSWORD
            {
                QLineEdit *pLineEdit = new QLineEdit( pwidgetParent );
                if ( pProperty->nPromptType == ODBCINST_PROMPTTYPE_TEXTEDIT_PASSWORD )
                    pLineEdit->setEchoMode( QLineEdit::Password ) ;
                return pLineEdit;
            }
            break;
    }

    return 0;
}

void CPropertiesDelegate::setEditorData( QWidget *pwidgetEditor, const QModelIndex &modelindex ) const
{
    HODBCINSTPROPERTY pProperty = modelindex.model()->data( modelindex, Qt::EditRole ).value<HODBCINSTPROPERTY>();

    switch ( pProperty->nPromptType )
    {
        case ODBCINST_PROMPTTYPE_LABEL:
            break;

        case ODBCINST_PROMPTTYPE_LISTBOX:
            {
                QComboBox *pComboBox = static_cast<QComboBox*>( pwidgetEditor );
                pComboBox->setCurrentIndex( pComboBox->findText( pProperty->szValue, Qt::MatchExactly ) );
            }
            break;

        case ODBCINST_PROMPTTYPE_COMBOBOX:
            {
                QComboBox *pComboBox = static_cast<QComboBox*>( pwidgetEditor );
                pComboBox->lineEdit()->setText( pProperty->szValue );
            }
            break;

        case ODBCINST_PROMPTTYPE_FILENAME:
            {
                CFileSelector *pFileSelector = static_cast<CFileSelector*>( pwidgetEditor );
                pFileSelector->setText( pProperty->szValue );
            }
            break;

        case ODBCINST_PROMPTTYPE_HIDDEN:
            break;

        default: // PROMPTTYPE_TEXTEDIT and PROMPTTYPE_TEXTEDIT_PASSWORD
            {
                QLineEdit *pLineEdit = static_cast<QLineEdit*>( pwidgetEditor );
                pLineEdit->setText( pProperty->szValue );
            }
            break;
    }
}

/* we could just update the HODBCINSTPROPERTY value here but we go with the Qt encouraged flow of things and defer to the views setData */
void CPropertiesDelegate::setModelData( QWidget *pwidgetEditor, QAbstractItemModel *abstractitemmodel, const QModelIndex &modelindex) const
{
    HODBCINSTPROPERTY pProperty = abstractitemmodel->data( modelindex, Qt::EditRole ).value<HODBCINSTPROPERTY>();

    switch ( pProperty->nPromptType )
    {
        case ODBCINST_PROMPTTYPE_LABEL:
            break;

        case ODBCINST_PROMPTTYPE_LISTBOX:
            {
                QComboBox *pComboBox = static_cast<QComboBox*>( pwidgetEditor );
                abstractitemmodel->setData( modelindex, pComboBox->currentText(), Qt::EditRole );
            }
            break;

        case ODBCINST_PROMPTTYPE_COMBOBOX:
            {
                QComboBox *pComboBox = static_cast<QComboBox*>( pwidgetEditor );
                abstractitemmodel->setData( modelindex, pComboBox->currentText(), Qt::EditRole );
            }
            break;

        case ODBCINST_PROMPTTYPE_FILENAME:
            {
                CFileSelector *pFileSelector = static_cast<CFileSelector*>( pwidgetEditor );
                abstractitemmodel->setData( modelindex, pFileSelector->getText(), Qt::EditRole );
            }
            break;

        case ODBCINST_PROMPTTYPE_HIDDEN:
            break;

        default: // PROMPTTYPE_TEXTEDIT and PROMPTTYPE_TEXTEDIT_PASSWORD
            {
                QLineEdit *pLineEdit = static_cast<QLineEdit*>( pwidgetEditor );
                abstractitemmodel->setData( modelindex, pLineEdit->text(), Qt::EditRole );
            }
            break;
    }
}

void CPropertiesDelegate::updateEditorGeometry( QWidget *pwidgetEditor, const QStyleOptionViewItem &styleoptionviewitem, const QModelIndex &modelindex ) const
{
    pwidgetEditor->setGeometry( styleoptionviewitem.rect );
}