source: trunk/demos/sqlbrowser/connectionwidget.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "connectionwidget.h"
43
44#include <QtGui>
45#include <QtSql>
46
47ConnectionWidget::ConnectionWidget(QWidget *parent)
48 : QWidget(parent)
49{
50 QVBoxLayout *layout = new QVBoxLayout(this);
51 tree = new QTreeWidget(this);
52 tree->setObjectName(QLatin1String("tree"));
53 tree->setHeaderLabels(QStringList(tr("database")));
54 tree->header()->setResizeMode(QHeaderView::Stretch);
55 QAction *refreshAction = new QAction(tr("Refresh"), tree);
56 metaDataAction = new QAction(tr("Show Schema"), tree);
57 connect(refreshAction, SIGNAL(triggered()), SLOT(refresh()));
58 connect(metaDataAction, SIGNAL(triggered()), SLOT(showMetaData()));
59 tree->addAction(refreshAction);
60 tree->addAction(metaDataAction);
61 tree->setContextMenuPolicy(Qt::ActionsContextMenu);
62
63 layout->addWidget(tree);
64
65 QMetaObject::connectSlotsByName(this);
66}
67
68ConnectionWidget::~ConnectionWidget()
69{
70}
71
72static QString qDBCaption(const QSqlDatabase &db)
73{
74 QString nm = db.driverName();
75 nm.append(QLatin1Char(':'));
76 if (!db.userName().isEmpty())
77 nm.append(db.userName()).append(QLatin1Char('@'));
78 nm.append(db.databaseName());
79 return nm;
80}
81
82void ConnectionWidget::refresh()
83{
84 tree->clear();
85 QStringList connectionNames = QSqlDatabase::connectionNames();
86
87 bool gotActiveDb = false;
88 for (int i = 0; i < connectionNames.count(); ++i) {
89 QTreeWidgetItem *root = new QTreeWidgetItem(tree);
90 QSqlDatabase db = QSqlDatabase::database(connectionNames.at(i), false);
91 root->setText(0, qDBCaption(db));
92 if (connectionNames.at(i) == activeDb) {
93 gotActiveDb = true;
94 setActive(root);
95 }
96 if (db.isOpen()) {
97 QStringList tables = db.tables();
98 for (int t = 0; t < tables.count(); ++t) {
99 QTreeWidgetItem *table = new QTreeWidgetItem(root);
100 table->setText(0, tables.at(t));
101 }
102 }
103 }
104 if (!gotActiveDb) {
105 activeDb = connectionNames.value(0);
106 setActive(tree->topLevelItem(0));
107 }
108
109 tree->doItemsLayout(); // HACK
110}
111
112QSqlDatabase ConnectionWidget::currentDatabase() const
113{
114 return QSqlDatabase::database(activeDb);
115}
116
117static void qSetBold(QTreeWidgetItem *item, bool bold)
118{
119 QFont font = item->font(0);
120 font.setBold(bold);
121 item->setFont(0, font);
122}
123
124void ConnectionWidget::setActive(QTreeWidgetItem *item)
125{
126 for (int i = 0; i < tree->topLevelItemCount(); ++i) {
127 if (tree->topLevelItem(i)->font(0).bold())
128 qSetBold(tree->topLevelItem(i), false);
129 }
130
131 if (!item)
132 return;
133
134 qSetBold(item, true);
135 activeDb = QSqlDatabase::connectionNames().value(tree->indexOfTopLevelItem(item));
136}
137
138void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */)
139{
140
141 if (!item)
142 return;
143
144 if (!item->parent()) {
145 setActive(item);
146 } else {
147 setActive(item->parent());
148 emit tableActivated(item->text(0));
149 }
150}
151
152void ConnectionWidget::showMetaData()
153{
154 QTreeWidgetItem *cItem = tree->currentItem();
155 if (!cItem || !cItem->parent())
156 return;
157 setActive(cItem->parent());
158 emit metaDataRequested(cItem->text(0));
159}
160
161void ConnectionWidget::on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
162{
163 metaDataAction->setEnabled(current && current->parent());
164}
165
Note: See TracBrowser for help on using the repository browser.