kexi

KexiProjectSelector.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "KexiProjectSelector.h"
21
22#include <kexidb/drivermanager.h>
23#include <kexidb/connectiondata.h>
24#include "core/kexi.h"
25
26#include <tdeapplication.h>
27#include <kiconloader.h>
28#include <kmimetype.h>
29#include <tdelocale.h>
30#include <kdebug.h>
31
32#include <tqlabel.h>
33#include <tqpushbutton.h>
34#include <tqlayout.h>
35#include <tqlistview.h>
36
37#include <assert.h>
38
40class KexiProjectSelectorWidgetPrivate
41{
42public:
43 KexiProjectSelectorWidgetPrivate()
44 {
45 selectable = true;
46 }
47 TQPixmap fileicon, dbicon;
48 bool showProjectNameColumn : 1;
49 bool showConnectionColumns : 1;
50 bool selectable : 1;
51};
52
53/*================================================================*/
54
56class ProjectDataLVItem : public TQListViewItem
57{
58public:
59 ProjectDataLVItem(KexiProjectData *d,
60 const KexiDB::Driver::Info& info, KexiProjectSelectorWidget *selector )
61 : TQListViewItem(selector->list)
62 , data(d)
63 {
64 int colnum = 0;
65 const KexiDB::ConnectionData *cdata = data->constConnectionData();
66 if (selector->d->showProjectNameColumn)
67 setText(colnum++, data->caption()+" ");
68
69 setText(colnum++, data->databaseName()+" ");
70
71 if (selector->d->showConnectionColumns) {
72 TQString drvname = info.caption.isEmpty() ? cdata->driverName : info.caption;
73 if (info.fileBased) {
74 setText(colnum++, i18n("File") + " ("+drvname+") " );
75 } else {
76 setText(colnum++, drvname+" " );
77 }
78
79 TQString conn;
80 if (!cdata->caption.isEmpty())
81 conn = cdata->caption + ": ";
82 conn += cdata->serverInfoString();
83 setText(3, conn + " ");
84 }
85 }
86 ~ProjectDataLVItem() {}
87
88 KexiProjectData *data;
89};
90
91/*================================================================*/
92
98 TQWidget* parent, const char* name,
99 KexiProjectSet* prj_set, bool showProjectNameColumn,
100 bool showConnectionColumns )
101 : KexiProjectSelectorBase( parent, name )
102 ,m_prj_set(prj_set)
103 ,d(new KexiProjectSelectorWidgetPrivate())
104{
105 d->showProjectNameColumn = showProjectNameColumn;
106 d->showConnectionColumns = showConnectionColumns;
107 TQString none, iconname = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0);
108 d->fileicon = TDEGlobal::iconLoader()->loadIcon( iconname, TDEIcon::Desktop );
109 setIcon( d->fileicon );
110 d->dbicon = SmallIcon("application-vnd.oasis.opendocument.database");
111// list->setHScrollBarMode( TQScrollView::AlwaysOn );
112
113 if (!d->showConnectionColumns) {
114 list->removeColumn(2);
115 list->removeColumn(2);
116 }
117 if (!d->showProjectNameColumn) {
118 list->removeColumn(0);
119 }
120 setFocusProxy(list);
121
122 //show projects
123 setProjectSet( m_prj_set );
124 connect(list,TQ_SIGNAL(doubleClicked(TQListViewItem*)),this,TQ_SLOT(slotItemExecuted(TQListViewItem*)));
125 connect(list,TQ_SIGNAL(returnPressed(TQListViewItem*)),this,TQ_SLOT(slotItemExecuted(TQListViewItem*)));
126 connect(list,TQ_SIGNAL(selectionChanged()),this,TQ_SLOT(slotItemSelected()));
127}
128
136
138{
139 ProjectDataLVItem *item = static_cast<ProjectDataLVItem*>(list->selectedItem());
140 if (item)
141 return item->data;
142 return 0;
143}
144
145void KexiProjectSelectorWidget::slotItemExecuted(TQListViewItem *item)
146{
147 if (!d->selectable)
148 return;
149 ProjectDataLVItem *ditem = static_cast<ProjectDataLVItem*>(item);
150 if (ditem)
151 emit projectExecuted( ditem->data );
152}
153
154void KexiProjectSelectorWidget::slotItemSelected()
155{
156 if (!d->selectable)
157 return;
158 ProjectDataLVItem *ditem = static_cast<ProjectDataLVItem*>(list->selectedItem());
159 emit selectionChanged( ditem ? ditem->data : 0 );
160}
161
163{
164 if (prj_set) {
165 //old list
166 list->clear();
167 }
168 m_prj_set = prj_set;
169 if (!m_prj_set)
170 return;
171//TODO: what with project set's ownership?
172 if (m_prj_set->error()) {
173 kdDebug() << "KexiProjectSelectorWidget::setProjectSet() : m_prj_set->error() !"<<endl;
174 return;
175 }
176 KexiDB::DriverManager manager;
177 KexiProjectData::List prjlist = m_prj_set->list();
178 KexiProjectData *data = prjlist.first();
179 while (data) {
180 KexiDB::Driver::Info info = manager.driverInfo(data->constConnectionData()->driverName);
181 if (!info.name.isEmpty()) {
182 ProjectDataLVItem *item = new ProjectDataLVItem(data, info, this);
183 if (!d->selectable)
184 item->setSelectable(false);
185 if (info.fileBased)
186 item->setPixmap( 0, d->fileicon );
187 else
188 item->setPixmap( 0, d->dbicon );
189 }
190 else {
191 kdWarning() << "KexiProjectSelector::KexiProjectSelector(): no driver found for '"
192 << data->constConnectionData()->driverName << "'!" << endl;
193 }
194 data=prjlist.next();
195 }
196 if (list->firstChild()) {
197 list->setSelected(list->firstChild(),true);
198 }
199}
200
202{
203 if (d->selectable == set)
204 return;
205 d->selectable = set;
206 //update items' state
207 TQListViewItemIterator it( list );
208 while ( it.current() ) {
209 it.current()->setSelectable( d->selectable );
210 }
211}
212
214{
215 return d->selectable;
216}
217
218/*================================================================*/
219
221 KexiProjectSet* prj_set, bool showProjectNameColumn, bool showConnectionColumns)
222 : KDialogBase( Plain, i18n("Open Recent Project"),
223#ifndef KEXI_NO_UNFINISHED
225 Help |
226#endif
227 Ok | Cancel, Ok, parent, name )
228{
229 init(prj_set, showProjectNameColumn, showConnectionColumns);
230}
231
234 bool showProjectNameColumn, bool showConnectionColumns)
235 : KDialogBase(
236 Plain, i18n("Open Project"),
237#ifndef KEXI_NO_UNFINISHED
239 Help |
240#endif
241 Ok | Cancel, Ok, parent, name, true/*modal*/, false/*sep*/ )
242{
243 setButtonGuiItem(Ok, KGuiItem(i18n("&Open"), "document-open", i18n("Open Database Connection")));
244 assert(cdata);
245 if (!cdata)
246 return;
247 KexiProjectSet *prj_set = new KexiProjectSet( *cdata );
248 init(prj_set, showProjectNameColumn, showConnectionColumns);
249
250 m_sel->label->setText( i18n("Select a project on <b>%1</b> database server to open:")
251 .arg(cdata->serverInfoString(false)) );
252}
253
254KexiProjectSelectorDialog::~KexiProjectSelectorDialog()
255{
256}
257
258void KexiProjectSelectorDialog::init(KexiProjectSet* prj_set, bool showProjectNameColumn,
259 bool showConnectionColumns)
260{
261 setSizeGripEnabled(true);
262
263 TQVBoxLayout *lyr = new TQVBoxLayout(plainPage(), 0, KDialogBase::spacingHint(), "lyr");
264 m_sel = new KexiProjectSelectorWidget(plainPage(), "sel",
265 prj_set, showProjectNameColumn, showConnectionColumns);
266 lyr->addWidget(m_sel);
267 setIcon(*m_sel->icon());
268 m_sel->setFocus();
269
270 connect(m_sel,TQ_SIGNAL(projectExecuted(KexiProjectData*)),
271 this,TQ_SLOT(slotProjectExecuted(KexiProjectData*)));
272 connect(m_sel,TQ_SIGNAL(selectionChanged(KexiProjectData*)),
273 this,TQ_SLOT(slotProjectSelectionChanged(KexiProjectData*)));
274}
275
277{
278 return m_sel->selectedProjectData();
279}
280
281void KexiProjectSelectorDialog::slotProjectExecuted(KexiProjectData*)
282{
283 accept();
284}
285
286void KexiProjectSelectorDialog::slotProjectSelectionChanged(KexiProjectData* pdata)
287{
288 enableButtonOK(pdata);
289}
290
291void KexiProjectSelectorDialog::show()
292{
293 KDialogBase::show();
294 KDialog::centerOnScreen(this);
295}
296
297#include "KexiProjectSelector.moc"
TQString caption
The caption of the connection.
TQString driverName
the name of the driver that should be used to create a connection
Database specific connection data, e.g. host, port.
TQString serverInfoString(bool addUser=true) const
A user-friendly string for the server.
Database driver management, e.g. finding and loading drivers.
KexiDB::Driver::Info driverInfo(const TQString &name)
bool fileBased
true is the driver is for file-based database backend
Definition driver.h:78
static TQString defaultFileBasedDriverMimeType()
Definition driver.cpp:137
Kexi project core data member.
KexiProjectSelectorDialog(TQWidget *parent, const char *name, KexiProjectSet *prj_set, bool showProjectNameColumn=true, bool showConnectionColumns=true)
KexiProjectData * selectedProjectData() const
KexiProjectSelectorWidget(TQWidget *parent=0, const char *name=0, KexiProjectSet *prj_set=0, bool showProjectNameColumn=true, bool showConnectionColumns=true)
KexiProjectData * selectedProjectData() const
void setProjectSet(KexiProjectSet *prj_set)
Stores information about multiple kexi project-data items.