[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klfstylemanager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfstylemanager.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2007 by Philippe Faist
00005  *   philippe.faist@bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
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, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfstylemanager.cpp 399 2010-06-28 21:10:25Z philippe $ */
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 #include <QApplication>
00028 #include <QList>
00029 #include <QPushButton>
00030 #include <QMessageBox>
00031 #include <QInputDialog>
00032 #include <QDrag>
00033 #include <QMimeData>
00034 
00035 #include <ui_klfstylemanager.h>
00036 
00037 #include "klfstylemanager.h"
00038 
00039 
00040 
00041 Qt::ItemFlags KLFStyleListModel::flags(const QModelIndex& index) const
00042 {
00043   if (!index.isValid() || index.row() >= rowCount() || index.model() != this)
00044       return Qt::ItemIsDropEnabled; // we allow drops outside the items
00045   return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
00046 }
00047 
00048 QString KLFStyleListModel::styleName(int row) const
00049 {
00050   return data(index(row), Qt::DisplayRole).toString();
00051 }
00052 
00053 void KLFStyleListModel::setStyleName(int row, const QString& newname)
00054 {
00055   setData(index(row), newname, Qt::DisplayRole);
00056 }
00057 
00058 
00059 Qt::DropActions KLFStyleListModel::supportedDropActions() const
00060 {
00061   return Qt::CopyAction;
00062 }
00063 
00064 QStringList KLFStyleListModel::mimeTypes() const
00065 {
00066   QStringList types;
00067   types << "application/x-klf-stylename";
00068   return types;
00069 }
00070 
00071 QMimeData *KLFStyleListModel::mimeData(const QModelIndexList& indexes) const
00072 {
00073   QMimeData *mimeData = new QMimeData();
00074 
00075   // can only drag ONE stylename
00076   if (indexes.size() > 1 || indexes.size() <= 0) {
00077     return mimeData;
00078   }
00079 
00080   QByteArray encodedData;
00081   QDataStream stream(&encodedData, QIODevice::WriteOnly);
00082   stream << styleName(indexes[0].row());
00083   mimeData->setData("application/x-klf-stylename", encodedData);
00084   return mimeData;
00085 }
00086 
00087 bool KLFStyleListModel::dropMimeData(const QMimeData *mdata, Qt::DropAction action, int row, int column,
00088                                      const QModelIndex &parent)
00089 {
00090   if (action == Qt::IgnoreAction)
00091     return true;
00092 
00093   if (parent.isValid())
00094     return false;
00095 
00096   if (!mdata->hasFormat("application/x-klf-stylename"))
00097     return false;
00098 
00099   if (column > 0)
00100     return false;
00101 
00102   if (row == -1)
00103     row = rowCount();
00104 
00105   QByteArray encodedData = mdata->data("application/x-klf-stylename");
00106   QDataStream stream(&encodedData, QIODevice::ReadOnly);
00107   QString newItem;
00108 
00109   stream >> newItem;
00110 
00111   // find style already existant in this list
00112   int k;
00113   for (k = 0; k < rowCount() && styleName(k) != newItem; ++k)
00114     ;
00115   if (k >= rowCount()) {
00116     fprintf(stderr, "WARNING: Ignoring drop of style named `%s' which was not already in list!\n",
00117             newItem.toLocal8Bit().constData());
00118     return false;
00119   }
00120   // remove row at position k
00121   removeRows(k, 1);
00122   if (row > k)
00123     --row;
00124   // and insert our text at the right position
00125   insertRows(row, 1);
00126   setStyleName(row, newItem);
00127 
00128   emit internalMoveCompleted(k, row);
00129 
00130   return true;
00131 }
00132 
00133 
00134 // ------------------
00135 
00136 
00137 
00138 KLFStyleManager::KLFStyleManager(KLFStyleList *stydata, QWidget *parent)
00139   : QWidget(parent, Qt::Dialog)
00140 {
00141   u = new Ui::KLFStyleManager;
00142   u->setupUi(this);
00143   setObjectName("KLFStyleManager");
00144 
00145   _styptr = stydata;
00146 
00147   _drag_item = 0;
00148   _drag_init_pos = QPoint(-1,-1);
00149   /*  mDropIndicatorItem = 0; */
00150 
00151   mActionsPopup = new QMenu(this);
00152 
00155   actPopupDelete = mActionsPopup->addAction("", this, SLOT(slotDelete()));
00156   actPopupMoveUp = mActionsPopup->addAction("", this, SLOT(slotMoveUp()));
00157   actPopupMoveDown = mActionsPopup->addAction("", this, SLOT(slotMoveDown()));
00158   actPopupRename = mActionsPopup->addAction("", this, SLOT(slotRename()));
00159   u->btnActions->setMenu(mActionsPopup);
00160 
00161   mStyleListModel = new KLFStyleListModel(this);
00162   u->lstStyles->setModel(mStyleListModel);
00163 
00164   // populate style list
00165   slotRefresh();
00166 
00167   // and set menu items enabled or not
00168   refreshActionsEnabledState();
00169 
00170   connect(u->btnClose, SIGNAL(clicked()), this, SLOT(hide()));
00171 
00172   u->lstStyles->installEventFilter(this);
00173   connect(u->lstStyles, SIGNAL(customContextMenuRequested(const QPoint&)),
00174           this, SLOT(showActionsContextMenu(const QPoint&)));
00175   connect(mStyleListModel, SIGNAL(internalMoveCompleted(int, int)),
00176           this, SLOT(slotModelMoveCompleted(int, int)));
00177   connect(u->lstStyles->selectionModel(),
00178           SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
00179           this, SLOT(refreshActionsEnabledState()));
00180 
00181   retranslateUi(false);
00182 }
00183 
00184 
00185 void KLFStyleManager::retranslateUi(bool alsoBaseUi)
00186 {
00187   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME);
00188   if (alsoBaseUi)
00189     u->retranslateUi(this);
00190 
00191   actPopupDelete->setText(tr("Delete Style"));
00192   actPopupMoveUp->setText(tr("Move up"));
00193   actPopupMoveDown->setText(tr("Move down"));
00194   actPopupRename->setText(tr("Rename style"));
00195 }
00196 
00197 KLFStyleManager::~KLFStyleManager()
00198 {
00199 }
00200 
00201 void KLFStyleManager::refreshActionsEnabledState()
00202 {
00203   int curidx = currentRow();
00204 
00205   if (curidx != -1) {
00206     actPopupDelete->setEnabled(true);
00207     actPopupRename->setEnabled(true);
00208     actPopupMoveUp->setEnabled(curidx > 0);
00209     actPopupMoveDown->setEnabled(curidx < mStyleListModel->rowCount()-1);
00210   } else {
00211     actPopupDelete->setEnabled(false);
00212     actPopupRename->setEnabled(false);
00213     actPopupMoveUp->setEnabled(false);
00214     actPopupMoveDown->setEnabled(false);
00215   }
00216 }
00217 
00218 void KLFStyleManager::showActionsContextMenu(const QPoint& pos)
00219 {
00220   mActionsPopup->exec(u->lstStyles->mapToGlobal(pos));
00221 }
00222 
00223 
00224 int KLFStyleManager::currentRow()
00225 {
00226   QModelIndexList sel = u->lstStyles->selectionModel()->selectedRows();
00227   if (sel.size() == 0)
00228     return -1;
00229   if (sel.size() >= 2) {
00230     qWarning("Multiple style names selected! Expected Single Selection Policy!\n");
00231     return -1;
00232   }
00233   return sel[0].row();
00234 }
00235 
00236 void KLFStyleManager::slotDelete()
00237 {
00238   int r = currentRow();
00239   if ( r == -1 )
00240     return;
00241 
00242   if ( QMessageBox::question(this, tr("Erase style?"), tr("Are you sure you want to erase selected style?"),
00243                              QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes ) {
00244     _styptr->removeAt(r);
00245     mStyleListModel->removeRows(r, 1);
00246   }
00247 
00248   emit refreshStyles();
00249   refreshActionsEnabledState();
00250 }
00251 
00252 void KLFStyleManager::slotRename()
00253 {
00254   int r = currentRow();
00255   if ( r == -1 )
00256     return;
00257 
00258   QString newname = QInputDialog::getText(this, tr("Rename style"), tr("Enter new style name:"), QLineEdit::Normal,
00259                                           _styptr->at(r).name);
00260 
00261   if ( ! newname.isEmpty() ) {
00262     _styptr->operator[](r).name = newname;
00263     mStyleListModel->setStyleName(r, newname);
00264   }
00265 
00266   emit refreshStyles();
00267   refreshActionsEnabledState();
00268 }
00269 
00270 void KLFStyleManager::slotMoveUp()
00271 {
00272   int r = currentRow();
00273   if ( r < 1 || r >= mStyleListModel->rowCount() )
00274     return;
00275 
00276   QString s = mStyleListModel->styleName(r);
00277   mStyleListModel->setStyleName(r, mStyleListModel->styleName(r-1));
00278   mStyleListModel->setStyleName(r-1, s);
00279   slotModelMoveCompleted(r, r-1);
00280 
00281   emit refreshStyles();
00282   refreshActionsEnabledState();
00283 }
00284 
00285 void KLFStyleManager::slotMoveDown()
00286 {
00287   int r = currentRow();
00288   if ( r < 0 || r > mStyleListModel->rowCount() - 1 )
00289     return;
00290 
00291   QString s = mStyleListModel->styleName(r);
00292   mStyleListModel->setStyleName(r, mStyleListModel->styleName(r+1));
00293   mStyleListModel->setStyleName(r+1, s);
00294   slotModelMoveCompleted(r, r+1);
00295 
00296   emit refreshStyles();
00297   refreshActionsEnabledState();
00298 }
00299 
00300 
00301 void KLFStyleManager::slotModelMoveCompleted(int prev, int newpos)
00302 {
00303   KLFStyle sty = _styptr->takeAt(prev);
00304   _styptr->insert(newpos, sty);
00305 
00306   QModelIndex i = mStyleListModel->index(newpos);
00307   u->lstStyles->selectionModel()->select(i, QItemSelectionModel::ClearAndSelect);
00308   u->lstStyles->setCurrentIndex(i);
00309 
00310   emit refreshStyles();
00311   refreshActionsEnabledState();
00312 }
00313 
00314 void KLFStyleManager::slotRefresh()
00315 {
00316   QStringList list;
00317   for (int i = 0; i < _styptr->size(); ++i) {
00318     list << _styptr->at(i).name;
00319   }
00320   mStyleListModel->setStringList(list);
00321 }
00322 
00323 
00324 
00325 

Generated by doxygen 1.7.3