/* Klat - A LaTeX editor for KDE Copyright (C) 2002-2004 Jori Liesenborgs (jori@lumumba.luc.ac.be) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "klatexecsettingsdlgimpl.h" #include "klatexecsettingsdlgimpl.moc" #include "klateditcommanddlgimpl.h" #include #include KlatExecSettingsDlgImpl::KlatExecSettingsDlgImpl(QWidget *parent,const char *name) : KlatExecSettingsDlg(parent,name,true) { QObject::connect(addButton,SIGNAL(clicked()),this,SLOT(slotAdd())); QObject::connect(editButton,SIGNAL(clicked()),this,SLOT(slotEdit())); QObject::connect(deleteButton,SIGNAL(clicked()),this,SLOT(slotDelete())); QObject::connect(okButton,SIGNAL(clicked()),this,SLOT(accept())); QObject::connect(upButton,SIGNAL(clicked()),this,SLOT(slotUp())); QObject::connect(downButton,SIGNAL(clicked()),this,SLOT(slotDown())); QObject::connect(addSepButton,SIGNAL(clicked()),this,SLOT(slotAddSeparator())); QObject::connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject())); commandList->setSorting(-1); commandList->setSelectionMode(QListView::Single); } KlatExecSettingsDlgImpl::~KlatExecSettingsDlgImpl() { } int KlatExecSettingsDlgImpl::exec() { buildList(); return KlatExecSettingsDlg::exec(); } void KlatExecSettingsDlgImpl::slotAdd() { KlatEditCommandDlgImpl dlg(this); if (dlg.exec() != QDialog::Accepted) return; addMenuEntry(dlg.menuName(),dlg.commandLine(),dlg.saveFirst(),dlg.shortcut()); } void KlatExecSettingsDlgImpl::slotAddSeparator() { QString dummy; addMenuEntry(dummy,dummy,false,KShortcut()); } #define SAVEFIRSTYES "Yes" #define SAVEFIRSTNO "No" #define SEPARATOR "--------" void KlatExecSettingsDlgImpl::slotEdit() { QListViewItem *item = commandList->selectedItem(); int pos; if (!item) return; pos = getItemIndex(item); cfg.gotoEntry(pos); if (cfg.menuName().length() == 0) // it's a separator return; KlatEditCommandDlgImpl dlg(this); dlg.setMenuName(cfg.menuName()); dlg.setCommandLine(cfg.commandLine()); dlg.setSaveFirst(cfg.saveFirst()); dlg.setShortcut(cfg.shortcut()); if (dlg.exec() != QDialog::Accepted) return; QString menuname,cmdLine,savestr; KShortcut cut; bool savefirst; menuname = dlg.menuName(); cmdLine = dlg.commandLine(); savefirst = dlg.saveFirst(); cut = dlg.shortcut(); if (savefirst) savestr = SAVEFIRSTYES; else savestr = SAVEFIRSTNO; item->setText(0,menuname); item->setText(1,cut.toString()); item->setText(2,savestr); item->setText(3,cmdLine); cfg.changeCurrent(KlatExecMenuEntry(menuname,cmdLine,savefirst,cut)); } void KlatExecSettingsDlgImpl::slotDelete() { QListViewItem *item = commandList->selectedItem(); int pos; if (!item) return; pos = getItemIndex(item); cfg.gotoEntry(pos); cfg.deleteCurrent(); delete item; commandList->clearSelection(); } void KlatExecSettingsDlgImpl::slotUp() { QListViewItem *item = commandList->selectedItem(); QListViewItem *prev; int pos; if (!item) return; if ((prev = item->itemAbove()) == 0) return; pos = getItemIndex(item); if (prev->itemAbove() != 0) item->moveItem(prev->itemAbove()); else { commandList->takeItem(item); commandList->insertItem(item); } commandList->setSelected(item,true); cfg.gotoEntry(pos); KlatExecMenuEntry e = cfg.entry(); cfg.deleteCurrent(); cfg.gotoEntry(pos-2); cfg.insertAfterCurrent(e); } void KlatExecSettingsDlgImpl::slotDown() { QListViewItem *item = commandList->selectedItem(); QListViewItem *next; int pos; if (!item) return; if ((next = item->itemBelow()) == 0) return; pos = getItemIndex(item); item->moveItem(next); commandList->setSelected(item,true); cfg.gotoEntry(pos); KlatExecMenuEntry e = cfg.entry(); cfg.deleteCurrent(); cfg.gotoEntry(pos); cfg.insertAfterCurrent(e); } void KlatExecSettingsDlgImpl::buildList() { QListViewItem *previtem,*item; previtem = 0; commandList->clear(); if (!cfg.gotoFirstEntry()) return; do { QString menuname,cmdline,savefirst; KShortcut cut; if (cfg.menuName().length() == 0) menuname = SEPARATOR; else { menuname = cfg.menuName(); cmdline = cfg.commandLine(); if (cfg.saveFirst()) savefirst = SAVEFIRSTYES; else savefirst = SAVEFIRSTNO; cut = cfg.shortcut(); } item = new QListViewItem(commandList,previtem); item->setText(0,menuname); item->setText(1,cut.toString()); item->setText(2,savefirst); item->setText(3,cmdline); previtem = item; } while (cfg.gotoNextEntry()); } int KlatExecSettingsDlgImpl::getSelectedPosition() { QListViewItem *item = commandList->selectedItem(); if (!item) return -1; return getItemIndex(item); } int KlatExecSettingsDlgImpl::getItemIndex(QListViewItem *item) { QListViewItem *tmp; int index = 0; bool found = false; tmp = commandList->firstChild(); while (!found && tmp != 0) { if (tmp == item) found = true; else { tmp = tmp->itemBelow(); index++; } } if (!found) return -1; return index; } void KlatExecSettingsDlgImpl::addMenuEntry(const QString &menuname,const QString &cmdLine,bool savefirst,const KShortcut &shortcut) { QString savestr; QListViewItem *item,*previtem; int pos; if (savefirst) savestr = SAVEFIRSTYES; else savestr = SAVEFIRSTNO; pos = getSelectedPosition(); cfg.gotoEntry(pos); cfg.insertAfterCurrent(KlatExecMenuEntry(menuname,cmdLine,savefirst,shortcut)); if (pos < 0) // insert at the beginning previtem = 0; else previtem = commandList->selectedItem(); item = new QListViewItem(commandList,previtem); if (menuname.length() == 0) item->setText(0,SEPARATOR); else { item->setText(0,menuname); item->setText(1,shortcut.toString()); item->setText(2,savestr); item->setText(3,cmdLine); } commandList->setSelected(item,true); }