KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.4KB

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