📄 optionwindow.cc
字号:
/** @file OptionWindow - widget for editing program options Options are arranged to tabs and it is ensured, that only one dialog at once is active (via Private constructor and static method to invoke the dialog, which will focus on existing dialog if it exists, instead of creating second one) @author Martin Petricek*/#include "booloption.h"#include "combooption.h"#include "fontoption.h"#include "intoption.h"#include "option.h"#include "optionwindow.h"#include "realoption.h"#include "settings.h"#include "stringoption.h"#include "util.h"#include <QLabel>#include <QLayout>#include <QPushButton>#include <QTabWidget>#include <assert.h>#include <stdlib.h>namespace gui {using namespace std;/** Default constructor of option window. The window is initially empty @param parent parent widget containing this control @param widgetName Identifier that will be used to load and save position and size of this window in settings. If not specified, position will not be loaded or saved @param windowTitle Title of the option window*/OptionWindow::OptionWindow(const QString &windowTitle,QWidget *parent /*=0*/,const QString &widgetName/*=QString::null*/) : QWidget(parent,Qt::Window) { QWidget::setAttribute(Qt::WA_DeleteOnClose); wName=widgetName; setWindowTitle(windowTitle); //create list of properties in this editor; list=new QStringList(); //create option dictionary items=new QMap<QString,Option*>(); //create labels dictionary labels=new QMap<QString,QLabel*>(); QGridLayout* grl_up=new QGridLayout(this); grl_up->setRowStretch(0,1); grl_up->setRowStretch(1,0); //create tab widget tab=new QTabWidget(this); grl_up->addWidget(tab,0,0); //Bottom part QFrame *low=new QFrame(this); grl_up->addWidget(low,1,0); QGridLayout* grl=new QGridLayout(low); grl->setColumnStretch(0,10); grl->setColumnStretch(1,1); grl->setColumnStretch(2,1); grl->setColumnStretch(3,1); grl->setSpacing(16); grl->setMargin(8); leftLabel=new QLabel("",low); grl->addWidget(leftLabel,0,0); QPushButton* btOk= new QPushButton(QObject::tr("&Ok"),low); QPushButton* btApply= new QPushButton(QObject::tr("&Apply"),low); QPushButton* btCancel=new QPushButton(QObject::tr("&Cancel"),low); grl->addWidget(btOk,0,1); grl->addWidget(btApply,0,2); grl->addWidget(btCancel,0,3); QObject::connect(btCancel, SIGNAL(clicked()), this, SLOT(close())); QObject::connect(btApply, SIGNAL(clicked()), this, SLOT(apply())); QObject::connect(btOk, SIGNAL(clicked()), this, SLOT(ok())); if (!wName.isNull()) { globalSettings->restoreWindow(this,wName); }}/** Called on pushing 'Apply' button */void OptionWindow::apply() { //save settings QMapIterator<QString,Option*> it(*items); while (it.hasNext()) { it.next(); Option* c=it.value(); c->writeValue(); } //Write settings to disk (editor may crash or be killed, right?) globalSettings->flush(); postApply();}/** Set text on label in left lower corner (to the left of Ok/Apply/Cancel buttons @param text New text to show*/void OptionWindow::setLeftCornerText(const QString &text) { leftLabel->setText(text);}/** Called after settings are saved to disk and flushed. Do some extra applying of settings if necessary (resizing widgets/toolbars, etc ...)*/void OptionWindow::postApply() { //Do nothing. Override with function that do anything useful}/** Called on pushing 'OK' button */void OptionWindow::ok() { apply(); //apply settings close(); //and close window}/** Add empty tab to option dialog @param name Label of the new tab @param makeSegments Make it possible to split tab in parts, at cost of using an extra widget @return Inner widget of the new tab */QWidget* OptionWindow::addTab(const QString name,bool makeSegments/*=false*/) { QFrame* grid; if (makeSegments) { QFrame* mGrid=new QFrame();//was QFrame(tab), workaround for bug in QTabWidget tab->addTab(mGrid,name); QGridLayout* mGridLayout=gridl[mGrid]=new QGridLayout(mGrid); grid=new QFrame(mGrid); masterGrid[grid]=mGrid; mGridLayout->addWidget(grid,0,0); nObjects[mGrid]=1; } else { grid=new QFrame();//was QFrame(tab), workaround for bug in QTabWidget tab->addTab(grid,name); } initGridFrame(grid); return grid;}/** Initialize grid for tab widget, so controls can be added there. @param grid Widget to initialize with grid layout*/void OptionWindow::initGridFrame(QWidget *grid) { QGridLayout* grl=gridl[grid]=new QGridLayout(grid); nObjects[grid]=0; grl->setSpacing(5); grl->setMargin(8); //set key column to be fixed and value column to be expandable grl->setColumnStretch(0,0); grl->setColumnStretch(1,1);}/** Add break to the option tab, breaking the column alignment at this point.<br> In fact, break the tab into two separate parts (but only second part can be splitted again).<br> Can be only applied to tabs that were created with makeSegments=true \see addTab @param otab Tab to break @return new value for tab - the lower half*/QWidget* OptionWindow::addBreak(QWidget *otab) { QFrame* mGrid=masterGrid[otab]; assert(mGrid); QFrame* grid=new QFrame(mGrid); gridl[mGrid]->addWidget(grid,nObjects[mGrid],0); nObjects[mGrid]++; initGridFrame(grid); masterGrid.remove(otab); masterGrid[grid]=mGrid; return grid;}/** add Option to the window @param otab Tab holding that option @param caption Label for this option @param opt Option to be added to this widget */void OptionWindow::addOption(QWidget *otab,const QString &caption,Option *opt) { QString key=opt->getName(); QLabel *label; label=new QLabel(caption,otab); int labelHeight=label->sizeHint().height(); int optHeight=opt->sizeHint().height(); int lineHeight=MAX(labelHeight,optHeight); gridl[otab]->setRowMinimumHeight(nObjects[otab],lineHeight); gridl[otab]->addWidget(label,nObjects[otab],0); gridl[otab]->addWidget(opt,nObjects[otab],1); label->setFixedHeight(lineHeight); opt->setFixedHeight(lineHeight); nObjects[otab]++; list->append(key); items->insert(key,opt); labels->insert(caption,label); opt->readValue(); opt->show(); label->show();}/** add any widget to option (typically some label) The widget will take one line @param otab Tab holding that option @param elem element to add */void OptionWindow::addWidget(QWidget *otab,QWidget *elem) {// int lineHeight=elem->sizeHint().height();// gridl[otab]->setRowSpacing(nObjects[otab],lineHeight); gridl[otab]->addWidget(elem,nObjects[otab],0,1,2);// elem->setFixedHeight(lineHeight); nObjects[otab]++; elem->show();}/** add description text to option @param otab Tab holding that option @param text Text to add. Using rich text format, so basically you can use HTML here */void OptionWindow::addText(QWidget *otab,const QString &text) { QLabel *txt=new QLabel(text,otab); txt->setTextFormat(Qt::RichText); addWidget(otab,txt);}/** Add padding to pad the tab window @param otab Tab holding that option */void OptionWindow::finishTab(QWidget *otab) { QFrame *pad=new QFrame(otab); pad->setMinimumHeight(1); pad->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::MinimumExpanding); addWidget(otab,pad);}/** add Option to the window (type of option is string) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param defValue Default value if option not found in configuration */void OptionWindow::addOption(QWidget *otab,const QString &caption,const QString &key,const QString &defValue/*=QString::null*/) { addOption(otab,caption,new StringOption(key,otab,defValue));}/** add Option to the window (type of option is font) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param defValue Default value if option not found in configuration */void OptionWindow::addOptionFont(QWidget *otab,const QString &caption,const QString &key,const QString &defValue/*=QString::null*/) { addOption(otab,caption,new FontOption(key,otab,defValue));}/** Add Option to the window (type of option is string, edited by combobox) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param values List of allowed values for this combobox @param descriptions List of value descriptions for this combobox, must correspond in number and order with values */void OptionWindow::addOptionCombo(QWidget *otab,const QString &caption,const QString &key,const QStringList &values,const QStringList &descriptions) { addOption(otab,caption,new ComboOption(values,descriptions,key,otab));}/** Add Option to the window (type of option is string, edited by combobox) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param values List of allowed values for this combobox */void OptionWindow::addOptionCombo(QWidget *otab,const QString &caption,const QString &key,const QStringList &values) { addOption(otab,caption,new ComboOption(values,key,otab));}/** add Option to the window (type of option is float) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option */void OptionWindow::addOptionFloat(QWidget *otab,const QString &caption,const QString &key) { addOption(otab,caption,new RealOption(key,otab));}/** add Option to the window (type of option is bool) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param defValue Default value if option not found in configuration */void OptionWindow::addOptionBool(QWidget *otab,const QString &caption,const QString &key,bool defValue/*=false*/) { addOption(otab,caption,new BoolOption(key,otab,defValue));}/** add Option to the window (type of option is int) @param otab Tab holding that option @param caption Label for this option @param key Key of the given option @param defValue Default value if option not found in configuration */void OptionWindow::addOptionInt(QWidget *otab,const QString &caption,const QString &key,int defValue/*=0*/) { addOption(otab,caption,new IntOption(key,otab,defValue));}/** Wipe all items, ending with empty dialog */void OptionWindow::clear() { gridl.clear(); nObjects.clear(); masterGrid.clear(); list->clear(); labels->clear(); items->clear(); while (QWidget *item=tab->widget(0)) { delete item; }}/** default destructor */OptionWindow::~OptionWindow() { if (!wName.isNull()) { globalSettings->saveWindow(this,wName); } delete labels; delete items; delete list;}} // namespace gui
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -