Turn songdb into a plugin, lots of tweaks, drag'n'drop of songs temporarily broken
[kworship.git] / kworship / songdb / KwSongdbPlugin.cpp
bloba567623dec1ac04b1a5f3581d37b9b467d4bf0cb
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwSongdbPlugin.cpp
22 * @brief Song database plugin.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbPlugin.h"
27 #include "KwSongdbTree.h"
28 #include "KwSongdb.h"
29 #include "KwSongdbSongEditDialog.h"
30 #include "KwSongdbSong.h"
31 #include "KwSongdbSongBooksEditWidget.h"
32 #include <KwApplication.h>
33 #include <kworship.h>
35 #include <KAction>
36 #include <KActionMenu>
37 #include <KActionCollection>
38 #include <KGenericFactory>
39 #include <KMenu>
40 #include <KMessageBox>
42 #include <QToolBar>
43 #include <QDockWidget>
44 #include <QVBoxLayout>
45 #include <QSqlError>
47 K_EXPORT_COMPONENT_FACTORY( kworship_songdb, KGenericFactory<KwSongdbPlugin>("kworship_songdb") )
50 * Constructors + destructor
53 /// Default constructor.
54 KwSongdbPlugin::KwSongdbPlugin(QObject* parent, const QStringList& params)
55 : KwPlugin(parent, "songdb",
56 i18n("Songdb"),
57 i18n("The song database plugin allows for the management and display of song lyrics."))
58 , m_songDbTree(0)
59 , m_unlockSongDbAction(0)
60 , m_lockSongDbAction(0)
61 , m_groupSongsByAction(0)
62 , m_addSongAction(0)
63 , m_editSongAction(0)
64 , m_editSongBooksAction(0)
66 setXMLFile("songdb/kworship_songdbui.rc");
68 // Set up actions
70 m_unlockSongDbAction = new KAction(KIcon("document-decrypt"), i18n("Unlock Song Database"), this);
71 actionCollection()->addAction( QLatin1String("unlock_song_database"), m_unlockSongDbAction);
72 connect(m_unlockSongDbAction, SIGNAL(triggered(bool)), this, SLOT(songdbUnlock()));
74 m_lockSongDbAction = new KAction(KIcon("document-encrypt"), i18n("Lock Song Database"), this);
75 m_lockSongDbAction->setVisible(false);
76 actionCollection()->addAction( QLatin1String("lock_song_database"), m_lockSongDbAction);
77 connect(m_lockSongDbAction, SIGNAL(triggered(bool)), this, SLOT(songdbLock()));
79 m_groupSongsByAction = new KActionMenu(KIcon("view-filter"), i18n("Group By"), this);
80 m_groupSongsByAction->setDelayed(false);
81 actionCollection()->addAction( QLatin1String("group_songs_by"), m_groupSongsByAction);
83 m_addSongAction = new KAction(KIcon("list-add"), i18n("Add Song"), this);
84 actionCollection()->addAction( QLatin1String("add_song"), m_addSongAction);
85 connect(m_addSongAction, SIGNAL(triggered(bool)), this, SLOT(songdbAdd()));
87 m_editSongAction = new KAction(KIcon("view-media-lyrics"), i18n("Edit Song"), this);
88 actionCollection()->addAction( QLatin1String("edit_song"), m_editSongAction);
89 connect(m_editSongAction, SIGNAL(triggered(bool)), this, SLOT(songdbEdit()));
91 m_editSongBooksAction = new KAction(KIcon("format-list-ordered"), i18n("Edit Song Books"), this);
92 actionCollection()->addAction( QLatin1String("edit_song_books"), m_editSongBooksAction);
93 connect(m_editSongBooksAction, SIGNAL(triggered(bool)), this, SLOT(songdbEditSongBooks()));
95 // Set up the docker
96 m_docker = new QDockWidget(i18n("Song DB"));
97 m_docker->setObjectName("dockSongdb");
98 QWidget* mainDockWidget = new QWidget(/*m_docker*/);
99 QVBoxLayout* dockLayout = new QVBoxLayout(mainDockWidget);
100 m_docker->setWidget(mainDockWidget);
102 // Ensure database is connected
103 KwApplication* app = KwApplication::self();
104 if (app->database().isOpen())
106 new KwSongdb(app->database());
108 // Toolbar widget
109 QWidget* toolbarWidget = new QWidget(mainDockWidget);
110 dockLayout->addWidget(toolbarWidget);
111 QHBoxLayout* toolbarLayout = new QHBoxLayout(toolbarWidget);
113 // Small toolbar for group by action
114 QToolBar* songTextToolBar = new QToolBar("Songs");
115 songTextToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
116 songTextToolBar->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
117 toolbarLayout->addWidget(songTextToolBar);
119 songTextToolBar->addAction(m_groupSongsByAction);
121 // Rest of the toolbar
122 QToolBar* songToolBar = new QToolBar("Songs");
123 songToolBar->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
124 toolbarLayout->addWidget(songToolBar);
126 songToolBar->addAction(m_addSongAction);
127 songToolBar->addAction(m_editSongAction);
128 songToolBar->addAction(m_editSongBooksAction);
129 KAction* insertIntoPlaylistAction = new KAction(KIcon("player_playlist"), i18n("Insert Into Playlist"), songToolBar);
130 songToolBar->addAction(insertIntoPlaylistAction);
132 KMenu* groupByMenu = new KMenu(songToolBar);
133 m_groupSongsByAction->setMenu(groupByMenu);
135 m_songDbTree = new KwSongdbTree(mainDockWidget);
136 dockLayout->addWidget(m_songDbTree);
137 groupByMenu->addActions(m_songDbTree->groupByActions()->actions());
139 else
141 QLabel* labelNoDb = new QLabel(i18n("Song database not connected: %1", app->database().lastError().text()), mainDockWidget);
142 dockLayout->addWidget(labelNoDb);
145 app->mainWindow()->addDockWidget(Qt::LeftDockWidgetArea, m_docker);
148 /// Destructor.
149 KwSongdbPlugin::~KwSongdbPlugin()
151 delete KwSongdb::self();
155 * Slots
158 void KwSongdbPlugin::songdbUnlock()
160 m_unlockSongDbAction->setVisible(false);
161 m_lockSongDbAction->setVisible(true);
164 void KwSongdbPlugin::songdbLock()
166 m_unlockSongDbAction->setVisible(true);
167 m_lockSongDbAction->setVisible(false);
170 void KwSongdbPlugin::songdbAdd()
172 KwSongdbSongEditDialog* dialog = new KwSongdbSongEditDialog(0);
173 dialog->setCaption(i18n("Add Song"));
174 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
175 dialog->show();
178 void KwSongdbPlugin::songdbEdit()
180 /// @todo Ensure the same song isn't open twice
181 KwSongdbSong* song = m_songDbTree->currentSong();
182 if (0 != song)
184 KwSongdbVersion* version = m_songDbTree->currentSongVersion();
185 // version may be 0
186 KwSongdbSongEditDialog* dialog = new KwSongdbSongEditDialog(song, version);
187 dialog->setCaption(i18n("Edit Song - %1", song->name()));
188 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
189 dialog->show();
191 else
193 KMessageBox::information(KwApplication::self()->mainWindow(), i18n("Please select a song."));
197 void KwSongdbPlugin::songdbEditSongBooks()
199 KwSongdbSongBooksEditWidget::showDialog();
203 * Loading and unloading virtual interface
206 void KwSongdbPlugin::_load()
210 void KwSongdbPlugin::_unload()