stationlistwidget.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 "stationlistwidget.h"
00022 
00023 #include <QTimer>
00024 #include <QContextMenuEvent>
00025 #include <KLocale>
00026 #include "settings_general.h"
00027 #define AND  &&
00028 #define OR  ||
00029 #define NOT  !
00030 #define EQUAL  ==
00031 
00032 stationlistWidget::stationlistWidget(QWidget *parent, QWidget *mainWindow)
00033   : QTableView(parent)
00034 {
00035   // variables
00036   QPointer<CustomizableHeaderView> theHorizontalHeader;
00037   int i;
00038   Qt::SortOrder order;
00039 
00040   // code
00041   theHorizontalHeader = new CustomizableHeaderView(Qt::Horizontal);
00042   // this also deletes automatically the previously used (standard) QHeaderView object:
00043   setHorizontalHeader(theHorizontalHeader);
00044   theHorizontalHeader->setContextMenuTitle(
00045     i18nc("@title:menu Title of the context menu where you can change visibility of columns",
00046           "Columns"));
00047   if (settings_general::sortingAscendingly()) {
00048     order = Qt::AscendingOrder;
00049   } else {
00050     order = Qt::DescendingOrder;
00051   };
00052   theHorizontalHeader->setSortIndicator(settings_general::sortByColumn(), order);
00053   theHorizontalHeader->setSortIndicatorShown(true);
00054   theHorizontalHeader->setClickable(true);
00055   /* We have to save the width of a column _befor_ it is hidden (after hiding,
00056   *  we wouln't be able to get the width anymore). */
00057   connect(theHorizontalHeader,
00058           SIGNAL(sectionAboutToBeHidden(int, Qt::Orientation)),
00059           this,
00060           SLOT(saveColumnSize(int)));
00061 
00062   setSelectionBehavior(QAbstractItemView::SelectRows);  // don't change this.
00063   // Much code relys on the assumption that only hole rows can be selected.
00064 
00065   setShowGrid(false);
00066 
00067   m_stationlistModel = new stationlistModel(this, mainWindow);
00068   setModel(m_stationlistModel);
00069 
00070   // restore column width
00071   for (i=0; i < m_stationlistModel->columnCount(); ++i) {
00072     setColumnHidden(
00073       i,
00074       !m_stationlistModel->columnInfo(stationlistModel::columnVisibility, i).toBool());
00075     setColumnWidth(
00076       i,
00077       m_stationlistModel->columnInfo(stationlistModel::columnWidth, i).toLongLong());
00078   };
00079 
00080   // various settings
00081   setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
00082   setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
00083   setDragDropMode(QAbstractItemView::DropOnly);
00084   setDragDropOverwriteMode(false); // don't override items, but insert new ones!
00085   setAcceptDrops(true);
00086   setDropIndicatorShown(false);
00087   setSortingEnabled(true);
00088 }
00089 
00090 stationlistWidget::~stationlistWidget()
00091 {
00092 }
00093 
00094 void stationlistWidget::saveAllColumnSizes()
00095 {
00096   // variables
00097   int i;
00098 
00099   // code
00100   for (i=0; i < m_stationlistModel->columnCount(); i++) {
00101     if (NOT isColumnHidden(i)) {
00102       saveColumnSize(i);
00103     };
00104     m_stationlistModel->columnInfo(stationlistModel::setColumnVisibility,
00105                                     i,
00106                                     -1,
00107                                     NOT isColumnHidden(i));
00108   };
00109 }
00110 
00111 void stationlistWidget::saveColumnSize(const int column)
00112 {
00113   m_stationlistModel->columnInfo(stationlistModel::setColumnWidth,
00114                                   column,
00115                                   -1,
00116                                   columnWidth(column));
00117 }
00118 
00119 void stationlistWidget::record()
00120 {
00121   // variables
00122   int i;
00123   // initialized with a list of the selected rows (for column 0):
00124   QModelIndexList m_list = selectionModel()->selectedRows();
00125 
00126   // code
00127   for (i = 0; i < m_list.size(); ++i) {
00128     m_stationlistModel->record(m_list.at(i).row());
00129   };
00130 }
00131 
00132 void stationlistWidget::stopRecord()
00133 {
00134   // variables
00135   int i;
00136   // initialized with a list of the selected rows (for column 0):
00137   QModelIndexList m_list = selectionModel()->selectedRows();
00138 
00139   // code
00140   for (i = 0; i < m_list.size(); ++i) {
00141     m_stationlistModel->stopRecording(m_list.at(i).row());
00142   };
00143 }
00144 
00145 void stationlistWidget::selectionChanged(const QItemSelection & selected,
00146                                           const QItemSelection & deselected)
00147 {
00148   // call the original function from the base class
00149   QTableView::selectionChanged(selected, deselected);
00150 
00151   // code
00152   switch (selectionModel()->selectedRows().size()) {
00153     case 0:
00154       emit noneSelected(true);
00155       break;
00156     case 1:
00157       emit oneSelected(true);
00158       break;
00159     default:
00160       emit multipleSelected(true);
00161       break;
00162   };
00163 }
00164 
00165 void stationlistWidget::displayStreamSettings()
00166 {
00167   // variables
00168   // initialized with a list of the selected rows (for column 0):
00169   QModelIndexList m_list = selectionModel()->selectedRows();
00170 
00171   // code
00172   if (m_list.size() == 1) {
00173     m_stationlistModel->showConfigDialog(m_list.at(0).row());
00174   };
00175 }
00176 
00177 void stationlistWidget::deleteStation()
00178 {
00179   // variables
00180   QModelIndexList m_list;
00181 
00182   // code
00183   // initialized with a list of the selected rows (for column 0):
00184   m_list = selectionModel()->selectedRows();
00185   while (m_list.size() > 0) {
00186     m_stationlistModel->removeRow(m_list.at(0).row());
00187     m_list = selectionModel()->selectedRows();
00188   };
00189 }
00190 
00191 void stationlistWidget::addNewStation()
00192 {
00193   // variables
00194   QModelIndex index_of_new_station;
00195 
00196   // code
00197   index_of_new_station = m_stationlistModel->addNewStation();
00198   if (index_of_new_station.isValid()) {
00199     clearSelection();
00200     selectionModel()->select(index_of_new_station,
00201                              QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
00202   };
00203 }
00204 
00205 void stationlistWidget::contextMenuEvent(QContextMenuEvent * e)
00206 {
00207   if (indexAt(e->pos()).isValid()) {
00208     streamContextMenu.exec(e->globalPos());
00209   } else {
00210     clearSelection();
00211     globalContextMenu.exec(e->globalPos());
00212   };
00213 }
00214 
00215 QPointer<stationlistModel> stationlistWidget::stationlistmodel()
00216 {
00217   return m_stationlistModel;
00218 }
00219 
00220 bool stationlistWidget::queryClose()
00221 {
00222   saveAllColumnSizes();
00223   return stationlistmodel()->queryClose();
00224 }
00225 
00226 void stationlistWidget::saveProperties(KConfigGroup & m_configGroup)
00227 {
00228   saveAllColumnSizes();
00229   stationlistmodel()->saveProperties(m_configGroup);
00230 }
00231 
00232 void stationlistWidget::readProperties(const KConfigGroup & m_configGroup)
00233 {
00234   stationlistmodel()->readProperties(m_configGroup);
00235 }
00236 
00237 void stationlistWidget::mousePressEvent(QMouseEvent *event)
00238 {
00239   if (event->button() == Qt::MidButton) {
00240     stationlistmodel()->pasteSelection();
00241   } else {
00242     // pass on other buttons to base class
00243     QTableView::mousePressEvent(event);
00244   };
00245 }

doxygen