mainwindow.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008  Tim Fechtner < urwald at users dot sourceforge dot net >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "mainwindow.h"
00022 
00023 #include <QClipboard>
00024 #include <QDockWidget>
00025 #include <QMimeData>
00026 #include <QTreeView>
00027 #include <KApplication>
00028 #include <KAction>
00029 #include <KLocale>
00030 #include <KActionCollection>
00031 #include <KTipDialog>
00032 #include <KStatusBar>
00033 #include <KStandardDirs>
00034 #include "icecast.h"
00035 #include "settings_general_dialog.h"
00036 #include "streamdirectoryproxymodel.h"
00037 
00038 MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)
00039 {
00040   //initialise central widget
00041   m_streamlistwidget = new stationlistWidget(this);
00042   setCentralWidget(m_streamlistwidget);
00043 
00044   //setup actions
00045   KStandardAction::preferences(this, SLOT(display_global_settings_dialog()),
00046                                 actionCollection());
00047 
00048   KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
00049 
00050   KAction * recordAction = new KAction(this);
00051   recordAction->setText(i18nc("@action", "&Record"));
00052   recordAction->setIcon(KIcon("media-record"));
00053   QList<QKeySequence> recordAction_tempShortcutList;
00054   recordAction_tempShortcutList.append(Qt::Key_R);
00055   recordAction_tempShortcutList.append(Qt::Key_MediaRecord);
00056   recordAction_tempShortcutList.append(Qt::CTRL + Qt::Key_R);
00057   recordAction->setShortcuts(recordAction_tempShortcutList);
00058   recordAction->setDisabled(true);
00059   connect(recordAction, SIGNAL(triggered(bool)),
00060            m_streamlistwidget, SLOT(record()));
00061   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00062            recordAction, SLOT(setDisabled(bool)));
00063   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00064            recordAction, SLOT(setEnabled(bool)));
00065   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00066            recordAction, SLOT(setEnabled(bool)));
00067   actionCollection()->addAction("record", recordAction);
00068   m_streamlistwidget->streamContextMenu.addAction(recordAction);
00069 
00070   KAction * stopRecordAction = new KAction(this);
00071   stopRecordAction->setText(i18nc("@action", "&Stop"));
00072   stopRecordAction->setIcon(KIcon("media-playback-stop"));
00073   QList<QKeySequence> stopRecordAction_tempShortcutList;
00074   stopRecordAction_tempShortcutList.append(Qt::Key_S);
00075   stopRecordAction_tempShortcutList.append(Qt::Key_MediaRecord);
00076   stopRecordAction_tempShortcutList.append(Qt::CTRL + Qt::Key_S);
00077   stopRecordAction_tempShortcutList.append(Qt::META + Qt::Key_V);
00078   stopRecordAction->setShortcuts(stopRecordAction_tempShortcutList);
00079   stopRecordAction->setDisabled(true);
00080   connect(stopRecordAction, SIGNAL(triggered(bool)),
00081            m_streamlistwidget, SLOT(stopRecord()));
00082   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00083            stopRecordAction, SLOT(setDisabled(bool)));
00084   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00085            stopRecordAction, SLOT(setEnabled(bool)));
00086   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00087            stopRecordAction, SLOT(setEnabled(bool)));
00088   actionCollection()->addAction("stopRecord", stopRecordAction);
00089   m_streamlistwidget->streamContextMenu.addAction(stopRecordAction);
00090 
00091   KAction * startListenInAction = new KAction(this);
00092   startListenInAction->setText(
00093     i18nc("@action listening in the stream that is actually recorded (and has been selected)",
00094           "&Listen in"));
00095   startListenInAction->setIcon(KIcon("audio-volume-high"));
00096   /*QList<QKeySequence> startListenInAction_tempShortcutList;
00097   startListenInAction_tempShortcutList.append(Qt::Key_S);
00098   startListenInAction_tempShortcutList.append(Qt::Key_MediaRecord);
00099   startListenInAction_tempShortcutList.append(Qt::CTRL + Qt::Key_S);
00100   startListenInAction_tempShortcutList.append(Qt::META + Qt::Key_V);
00101   startListenInAction->setShortcuts(startListenInAction_tempShortcutList);*/
00102   startListenInAction->setDisabled(true);
00103   connect(startListenInAction, SIGNAL(triggered(bool)),
00104           m_streamlistwidget->stationlistmodel(), SLOT(enableListeningIn()));
00105   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00106           startListenInAction, SLOT(setDisabled(bool)));
00107   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00108           startListenInAction, SLOT(setEnabled(bool)));
00109   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00110           startListenInAction, SLOT(setDisabled(bool)));
00111   actionCollection()->addAction("startListenIn", startListenInAction);
00112   m_streamlistwidget->streamContextMenu.addAction(startListenInAction);
00113 
00114   KAction * stopListenInAction = new KAction(this);
00115   stopListenInAction->setText(i18nc("@action listening in the stream that is actually recorded",
00116                                     "&Mute"));
00117   stopListenInAction->setIcon(KIcon("audio-volume-muted"));
00118   /*QList<QKeySequence> stopListenInAction_tempShortcutList;
00119   stopListenInAction_tempShortcutList.append(Qt::Key_S);
00120   stopListenInAction_tempShortcutList.append(Qt::Key_MediaRecord);
00121   stopListenInAction_tempShortcutList.append(Qt::CTRL + Qt::Key_S);
00122   stopListenInAction_tempShortcutList.append(Qt::META + Qt::Key_V);
00123   stopListenInAction->setShortcuts(stopListenInAction_tempShortcutList);*/
00124   stopListenInAction->setDisabled(true);
00125   connect(stopListenInAction, SIGNAL(triggered(bool)),
00126           m_streamlistwidget->stationlistmodel(), SLOT(disableListeningIn()));
00127   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00128           stopListenInAction, SLOT(setDisabled(bool)));
00129   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00130           stopListenInAction, SLOT(setEnabled(bool)));
00131   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00132           stopListenInAction, SLOT(setDisabled(bool)));
00133   actionCollection()->addAction("stopListenIn", stopListenInAction);
00134   m_streamlistwidget->streamContextMenu.addAction(stopListenInAction);
00135 
00136   KAction * newStreamAction = KStandardAction::openNew(m_streamlistwidget,
00137                                                        SLOT(addNewStation()),
00138                                                        this);
00139   newStreamAction->setText(i18nc("@action", "&New stream"));
00140   actionCollection()->addAction("newStream", newStreamAction);
00141   m_streamlistwidget->globalContextMenu.addAction(newStreamAction);
00142   m_streamlistwidget->streamContextMenu.addAction(newStreamAction);
00143 
00144   KAction * deleteStreamAction = new KAction(this);
00145   deleteStreamAction->setText(i18nc("@action", "&Delete stream"));
00146   deleteStreamAction->setIcon(KIcon("edit-delete"));
00147   deleteStreamAction->setShortcut(Qt::SHIFT + Qt::Key_Delete);
00148   deleteStreamAction->setDisabled(true);
00149   connect(deleteStreamAction, SIGNAL(triggered(bool)),
00150           m_streamlistwidget, SLOT(deleteStation()));
00151   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00152           deleteStreamAction, SLOT(setDisabled(bool)));
00153   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00154           deleteStreamAction, SLOT(setEnabled(bool)));
00155   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00156           deleteStreamAction, SLOT(setEnabled(bool)));
00157   actionCollection()->addAction("deleteStream", deleteStreamAction);
00158   m_streamlistwidget->streamContextMenu.addAction(deleteStreamAction);
00159 
00160   pasteAction = actionCollection()->addAction(KStandardAction::Paste,
00161                                               "paste",
00162                                               m_streamlistwidget->stationlistmodel(),
00163                                               SLOT(paste()));
00164   m_streamlistwidget->globalContextMenu.addAction(pasteAction);
00165   m_streamlistwidget->streamContextMenu.addAction(pasteAction);
00166   connect(QApplication::clipboard(), SIGNAL(dataChanged()),
00167           this, SLOT(reloadStatusOfPasteAction()));
00168   reloadStatusOfPasteAction();
00169 
00170   KAction * selectAllAction = KStandardAction::selectAll(m_streamlistwidget,
00171                                                          SLOT(selectAll()),
00172                                                          actionCollection());
00173   m_streamlistwidget->globalContextMenu.addAction(selectAllAction);
00174   m_streamlistwidget->streamContextMenu.addAction(selectAllAction);
00175 
00176   KAction * streamSettingsAction = new KAction(this);
00177   streamSettingsAction->setText(i18nc("@action properties of a stream)", "&Properties"));
00178   streamSettingsAction->setIcon(KIcon("document-properties"));
00179   QList<QKeySequence> streamSettingsAction_tempShortcutList;
00180   streamSettingsAction_tempShortcutList.append(Qt::ALT + Qt::Key_Return);
00181   streamSettingsAction_tempShortcutList.append(Qt::Key_MediaStop);
00182   streamSettingsAction->setShortcuts(streamSettingsAction_tempShortcutList);
00183   streamSettingsAction->setDisabled(true);
00184   connect(streamSettingsAction, SIGNAL(triggered(bool)),
00185            m_streamlistwidget, SLOT(displayStreamSettings()));
00186   connect(m_streamlistwidget, SIGNAL(noneSelected(bool)),
00187            streamSettingsAction, SLOT(setDisabled(bool)));
00188   connect(m_streamlistwidget, SIGNAL(oneSelected(bool)),
00189            streamSettingsAction, SLOT(setEnabled(bool)));
00190   connect(m_streamlistwidget, SIGNAL(multipleSelected(bool)),
00191            streamSettingsAction, SLOT(setDisabled(bool)));
00192   actionCollection()->addAction("streamSettings", streamSettingsAction);
00193   m_streamlistwidget->streamContextMenu.addAction(streamSettingsAction);
00194 
00195   KStandardAction::tipOfDay(this, SLOT(displayTipOfDay()), actionCollection());
00196 
00197   //setup GUI
00198   setupGUI();  // creates also some further actions automatically
00199   QAction *helpContentsAction = actionCollection()->action("help_contents");
00200   if (!(helpContentsAction == 0)) {
00201     helpContentsAction->setEnabled(false);
00202   };
00203 
00204   statusBar()->insertPermanentItem(QString(), 0);
00205   statusBar()->insertPermanentItem(QString(), 1);
00206   connect(m_streamlistwidget->stationlistmodel(),
00207            SIGNAL(bandwidthChanged()),
00208            this,
00209            SLOT(actualize_bandwidth_in_statusBar()));
00210   connect(m_streamlistwidget->stationlistmodel(),
00211            SIGNAL(numberOfActiveStreamsChanged()),
00212            this,
00213            SLOT(actualize_numberOfActiveStreams_in_statusBar()));
00214   actualize_bandwidth_in_statusBar();  // initialization
00215   actualize_numberOfActiveStreams_in_statusBar();  // initialization
00216 
00217   QDockWidget *m_streamDirectory = new QDockWidget(
00218     i18nc("@title:window Dock name of the directory of radio stations", "stream catalog"),
00219     this);
00220   m_streamDirectory->setObjectName("theRadiostationDirectory");  // to avoid a warning on stdout
00221   m_streamDirectory->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea |
00222     Qt::BottomDockWidgetArea);
00223   QTreeView *m_directoryView = new QTreeView();
00224   m_streamDirectory->setWidget(m_directoryView);
00225   icecast *m_icecast_model = new icecast(m_directoryView/*TODO, the_icon*/);
00226   streamDirectoryProxyModel *m_proxy_model = new streamDirectoryProxyModel(m_icecast_model);
00227   m_proxy_model->setSourceModel(m_icecast_model);
00228   m_directoryView->setModel(m_proxy_model);
00229   m_directoryView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
00230   m_directoryView->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
00231   m_directoryView->setAnimated(true);
00232   m_directoryView->setAlternatingRowColors(true);
00233   m_directoryView->setSortingEnabled(true);
00234   m_directoryView->setUniformRowHeights(true); // makes the widget more performant
00235   m_directoryView->setSelectionMode(QAbstractItemView::ExtendedSelection);
00236   m_directoryView->setDragEnabled(true);
00237   addDockWidget(Qt::LeftDockWidgetArea, m_streamDirectory);
00238 
00239   KTipDialog::showTip(this, "kradioripper/tips");
00240 }
00241 
00242 MainWindow::~MainWindow()
00243 {
00244 }
00245 
00246 void MainWindow::actualize_numberOfActiveStreams_in_statusBar()
00247 {
00248   statusBar()->changeItem(
00249     i18ncp("@info:status",
00250             "Recording %1 stream.",
00251             "Recording %1 streams.",
00252             m_streamlistwidget->stationlistmodel()->numberOfActiveStreams()),
00253     0);
00254 }
00255 
00256 void MainWindow::actualize_bandwidth_in_statusBar()
00257 {
00258   statusBar()->changeItem(
00259     i18ncp("@info:status The unit is kbit (=1000 bit) instead of Kibit (=1024 bit). "
00260               "See http://en.wikipedia.org/wiki/Binary_prefix for details.",
00261             "%1 kbit/s",
00262             "%1 kbit/s",
00263             m_streamlistwidget->stationlistmodel()->bandwidth()),
00264     1);
00265 }
00266 
00267 bool MainWindow::queryClose()
00268 {
00269   return m_streamlistwidget->queryClose();
00270 }
00271 
00272 void MainWindow::saveProperties(KConfigGroup & m_configGroup)
00273 {
00274   m_streamlistwidget->saveProperties(m_configGroup);
00275 }
00276 
00277 void MainWindow::readProperties(const KConfigGroup & m_configGroup)
00278 {
00279   m_streamlistwidget->readProperties(m_configGroup);
00280 }
00281 
00282 void MainWindow::display_global_settings_dialog()
00283 {
00284   // if the dialog object yet exists, we can display it directly and return.
00285   if (KConfigDialog::showDialog("settings_general_dialog")) {
00286     return;
00287   };
00288 
00289   // else we have to construct and configure it before displaying it and return
00290   settings_general_dialog * dialog = new settings_general_dialog(this,
00291                                                                   "settings_general_dialog");
00292   dialog->show();
00293 }
00294 
00295 void MainWindow::displayTipOfDay()
00296 {
00297   KTipDialog::showTip(this, "kradioripper/tips", true);
00298 }
00299 
00300 void MainWindow::reloadStatusOfPasteAction()
00301 {
00302   bool isAccepted = false;
00303   const QStringList clipboardFormats = QApplication::clipboard()->mimeData()->formats();
00304   const QStringList acceptedFormats = m_streamlistwidget->stationlistmodel()->mimeTypes();
00305   const int numberOfAcceptedFormats = acceptedFormats.size();
00306   int i = 0;
00307   while ((!isAccepted) && (i < numberOfAcceptedFormats)) {
00308     isAccepted = clipboardFormats.contains(acceptedFormats.at(i));
00309     ++i;
00310   };
00311   pasteAction->setEnabled(isAccepted);
00312 }

doxygen