Database: sqlite default to appdata kworship.db
[kworship.git] / kworship / songdb / KwSongdbSongBooksEditWidget.cpp
blob2ef06d9a14987e6912787d505093a9886dbe21d5
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 KwSongdbSongBooksEditWidget.cpp
22 * @brief Widget for editing song books.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbSongBooksEditWidget.h"
27 #include "KwSongdbSongBookItem.h"
28 #include "KwSongdb.h"
30 #include <KAction>
31 #include <KDialog>
32 #include <KLocale>
34 #include <QToolBar>
35 #include <QPointer>
38 * Static
41 /// Show the edit song books dialog.
42 KwSongdbSongBooksEditWidget* KwSongdbSongBooksEditWidget::showDialog()
44 static QPointer<KDialog> dialog;
45 static QPointer<KwSongdbSongBooksEditWidget> view;
47 if (0 == dialog)
49 dialog = new KDialog();
50 view = new KwSongdbSongBooksEditWidget();
51 dialog->setMainWidget(view);
52 dialog->setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
53 dialog->enableButtonApply(false);
55 connect(dialog, SIGNAL(applyClicked()),
56 view, SLOT(save()));
57 connect(dialog, SIGNAL(okClicked()),
58 view, SLOT(save()));
59 connect(view, SIGNAL(changed(bool)),
60 dialog, SLOT(enableButtonApply(bool)));
62 dialog->setWindowTitle(i18n("Edit Song Books"));
63 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
64 dialog->show();
66 else
68 dialog->activateWindow();
69 dialog->raise();
73 return view;
77 * Constructors + destructor
80 /// Default constructor.
81 KwSongdbSongBooksEditWidget::KwSongdbSongBooksEditWidget()
82 : QWidget()
83 , Ui::KwSongdbSongBooksEditWidget_base()
85 setupUi(this);
87 // Song books toolbar
88 QToolBar* songBooksToolBar = new QToolBar("songBooksToolBar");
89 layoutSongBooksToolBar->layout()->addWidget(songBooksToolBar);
91 KAction* addAction = new KAction(KIcon("list-add"), i18n("Add Song Book"), songBooksToolBar);
92 connect(addAction, SIGNAL(triggered(bool)),
93 this, SLOT(addSongBook()));
94 songBooksToolBar->addAction(addAction);
96 KAction* removeAction = new KAction(KIcon("list-remove"), i18n("Remove Song Book"), songBooksToolBar);
97 connect(removeAction, SIGNAL(triggered(bool)),
98 this, SLOT(removeSongBook()));
99 songBooksToolBar->addAction(removeAction);
102 // Signals
103 connect(listSongBooks, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
104 this, SLOT(songBookChanged(QListWidgetItem*, QListWidgetItem*)));
105 connect(editDescription, SIGNAL(textChanged()),
106 this, SLOT(descriptionChanged()));
108 QList<KwSongdbSongBook*> songBooks = KwSongdb::self()->songBooks();
109 foreach (KwSongdbSongBook* songBook, songBooks)
111 KwSongdbSongBookItem* item = new KwSongdbSongBookItem(songBook, listSongBooks);
112 Q_UNUSED(item);
116 /// Destructor.
117 KwSongdbSongBooksEditWidget::~KwSongdbSongBooksEditWidget()
122 * Saving and loading
125 /// Save to database.
126 void KwSongdbSongBooksEditWidget::save()
128 // Song books
129 for (int i = 0; i < listSongBooks->count(); ++i)
131 KwSongdbSongBookItem* item = dynamic_cast<KwSongdbSongBookItem*>(listSongBooks->item(i));
132 Q_ASSERT(0 != item);
133 item->save();
135 saved();
138 /// Add a song book.
139 void KwSongdbSongBooksEditWidget::addSongBook()
141 KwSongdbSongBookItem* item = new KwSongdbSongBookItem(listSongBooks);
142 listSongBooks->setCurrentItem(item);
146 * Private slots
149 /// A different song book has been selected.
150 void KwSongdbSongBooksEditWidget::songBookChanged(QListWidgetItem* current, QListWidgetItem* previous)
152 KwSongdbSongBookItem* previousVersion = dynamic_cast<KwSongdbSongBookItem*>(previous);
153 KwSongdbSongBookItem* currentVersion = dynamic_cast<KwSongdbSongBookItem*>(current);
155 if (0 != previousVersion)
157 disconnect(editAbreviation, SIGNAL(textEdited(const QString&)),
158 previousVersion, SLOT(setAbreviation(const QString&)));
159 disconnect(editName, SIGNAL(textEdited(const QString&)),
160 previousVersion, SLOT(setName(const QString&)));
161 disconnect(this, SIGNAL(descriptionChangedSignal(const QString&)),
162 previousVersion, SLOT(setDescription(const QString&)));
165 if (0 != currentVersion)
167 editAbreviation->setText(currentVersion->abreviation());
168 editName->setText(currentVersion->name());
169 editDescription->document()->setPlainText(currentVersion->description());
171 connect(editAbreviation, SIGNAL(textEdited(const QString&)),
172 currentVersion, SLOT(setAbreviation(const QString&)));
173 connect(editName, SIGNAL(textEdited(const QString&)),
174 currentVersion, SLOT(setName(const QString&)));
175 connect(this, SIGNAL(descriptionChangedSignal(const QString&)),
176 currentVersion, SLOT(setDescription(const QString&)));
178 groupSongBook->setEnabled(0 != currentVersion);
181 /// Description edit box has been modified.
182 void KwSongdbSongBooksEditWidget::descriptionChanged()
184 descriptionChangedSignal(editDescription->document()->toPlainText());
187 /// Remove the selected song book.
188 void KwSongdbSongBooksEditWidget::removeSongBook()