Qt 4.8
qaxselect.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the ActiveQt framework of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "qaxselect.h"
42 
43 #ifndef QT_NO_WIN_ACTIVEQT
44 
45 #include <qt_windows.h>
46 
48 
50 {
51 public:
54  {
55  HKEY classes_key;
56  RegOpenKeyEx(HKEY_CLASSES_ROOT, L"CLSID", 0, KEY_READ, &classes_key);
57  if (!classes_key)
58  return;
59 
60  DWORD index = 0;
61  LONG result = 0;
62  wchar_t buffer[256];
63  DWORD szBuffer = sizeof(buffer) / sizeof(wchar_t);
64  FILETIME ft;
65  do {
66  result = RegEnumKeyEx(classes_key, index, buffer, &szBuffer, 0, 0, 0, &ft);
67  szBuffer = sizeof(buffer) / sizeof(wchar_t);
68  if (result == ERROR_SUCCESS) {
69  HKEY sub_key;
70  QString clsid = QString::fromWCharArray(buffer);
71  result = RegOpenKeyEx(classes_key, reinterpret_cast<const wchar_t *>(QString(clsid + "\\Control").utf16()), 0, KEY_READ, &sub_key);
72  if (result == ERROR_SUCCESS) {
73  RegCloseKey(sub_key);
74  RegistryQueryValue(classes_key, buffer, (LPBYTE)buffer, &szBuffer);
76 
77  controls << name;
78  clsids.insert(name, clsid);
79  }
80  result = ERROR_SUCCESS;
81  }
82  szBuffer = sizeof(buffer) / sizeof(wchar_t);
83  ++index;
84  } while (result == ERROR_SUCCESS);
85  RegCloseKey(classes_key);
86  controls.sort();
87  }
88 
89  LONG RegistryQueryValue(HKEY hKey, LPCWSTR lpSubKey, LPBYTE lpData, LPDWORD lpcbData)
90  {
91  LONG ret = ERROR_FILE_NOT_FOUND;
92  HKEY hSubKey = NULL;
93  RegOpenKeyEx(hKey, lpSubKey, 0, KEY_READ, &hSubKey);
94  if (hSubKey) {
95  ret = RegQueryValueEx(hSubKey, 0, 0, 0, lpData, lpcbData);
96  RegCloseKey(hSubKey);
97  }
98  return ret;
99  }
100 
101  int rowCount(const QModelIndex & = QModelIndex()) const { return controls.count(); }
102  QVariant data(const QModelIndex &index, int role) const;
103 
104 private:
107 };
108 
110 {
111  if (!index.isValid())
112  return QVariant();
113 
114  if (role == Qt::DisplayRole)
115  return controls.at(index.row());
116  if (role == Qt::UserRole)
117  return clsids.value(controls.at(index.row()));
118 
119  return QVariant();
120 }
121 
122 QAxSelect::QAxSelect(QWidget *parent, Qt::WindowFlags f)
123 : QDialog(parent, f)
124 {
125 #ifndef QT_NO_CURSOR
127 #endif
128 
129  setupUi(this);
130  ActiveXList->setModel(new ControlList(this));
131  connect(ActiveXList->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
133 #ifndef QT_NO_CURSOR
135 #endif
136  ActiveXList->setFocus();
137 
138  connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
139  connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
140 }
141 
143 {
144  QVariant clsid = ActiveXList->model()->data(index, Qt::UserRole);
145  ActiveX->setText(clsid.toString());
146 }
147 
149 {
150  QVariant clsid = ActiveXList->model()->data(index, Qt::UserRole);
151  ActiveX->setText(clsid.toString());
152 
153  accept();
154 }
155 
157 #endif // QT_NO_WIN_ACTIVEQT
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual void accept()
Hides the modal dialog and sets the result code to Accepted.
Definition: qdialog.cpp:639
virtual void reject()
Hides the modal dialog and sets the result code to Rejected.
Definition: qdialog.cpp:650
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensio...
QAxSelect(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: qaxselect.cpp:122
The QString class provides a Unicode character string.
Definition: qstring.h:83
ControlList(QObject *parent=0)
Definition: qaxselect.cpp:52
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void on_ActiveXList_clicked(const QModelIndex &index)
Definition: qaxselect.cpp:142
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
LONG RegistryQueryValue(HKEY hKey, LPCWSTR lpSubKey, LPBYTE lpData, LPDWORD lpcbData)
Definition: qaxselect.cpp:89
QStringList controls
Definition: qaxselect.cpp:105
QMap< QString, QString > clsids
Definition: qaxselect.cpp:106
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void sort()
Sorts the list of strings in ascending order (case sensitively).
Definition: qstringlist.h:152
QVariant data(const QModelIndex &index, int role) const
Returns the data stored under the given role for the item referred to by the index.
Definition: qaxselect.cpp:109
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
int row() const
Returns the row this model index refers to.
const char * name
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
int rowCount(const QModelIndex &=QModelIndex()) const
Returns the number of rows under the given parent.
Definition: qaxselect.cpp:101
void on_ActiveXList_doubleClicked(const QModelIndex &index)
Definition: qaxselect.cpp:148
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QModelIndex class is used to locate data in a data model.
quint16 index
QString clsid() const
Definition: qaxselect.h:64
static void setOverrideCursor(const QCursor &)
Use changeOverrideCursor(cursor) (if replace is true) or setOverrideCursor(cursor) (if replace is fal...
static void restoreOverrideCursor()
Undoes the last setOverrideCursor().
void setupUi(QWidget *widget)
Sets up the user interface for the specified widget.