📄 bcuniteditwidget.cpp
字号:
/*************************************************************************** bcuniteditwidget.cpp ------------------- begin : Wed Sep 26 2001 copyright : (C) 2001 by Robby Stephenson email : robby@periapsis.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of version 2 of the GNU General Public License as * * published by the Free Software Foundation; * * * ***************************************************************************/#include "bcuniteditwidget.h"#include "bcunit.h"#include "bccollection.h"#include "bcattribute.h"#include "bcdetailedlistview.h"#include "bctabcontrol.h"#include "isbnvalidator.h"#include "bookcase.h"#include "bcutils.h"#include <kcompletion.h>#include <kbuttonbox.h>#include <klocale.h>#include <kdebug.h>#include <kmessagebox.h>#include <qlayout.h>#include <qstringlist.h>#include <qlabel.h>#include <qlistview.h>#include <qptrlist.h>#include <qgrid.h>#include <qpushbutton.h>#include <qwhatsthis.h>#include <qvbox.h>//#define SHOW_COPY_BTN// must be an even numberstatic const int NCOLS = 4;BCUnitEditWidget::BCUnitEditWidget(QWidget* parent_, const char* name_/*=0*/) : QWidget(parent_, name_), m_currColl(0), m_currUnit(0), m_tabs(new BCTabControl(this)), m_modified(false) {// kdDebug() << "BCUnitEditWidget()" << endl; QVBoxLayout* topLayout = new QVBoxLayout(this); connect(m_tabs, SIGNAL(tabSelected(int)), SLOT(slotSwitchFocus(int))); topLayout->setSpacing(5); topLayout->setMargin(5); // stretch = 1 so that the tabs expand vertically topLayout->addWidget(m_tabs, 1); KButtonBox* bb = new KButtonBox(this); bb->addStretch(); m_new = bb->addButton(i18n("New Book"), this, SLOT(slotHandleNew()));#ifdef SHOW_COPY_BTN m_copy = bb->addButton(i18n("Duplicate Book"), this, SLOT(slotHandleCopy()));#endif m_save = bb->addButton(i18n("Enter Book"), this, SLOT(slotHandleSave())); m_delete = bb->addButton(i18n("Delete Book"), this, SLOT(slotHandleDelete()));// m_clear = bb->addButton(i18n("Clear Data"), this, SLOT(slotHandleClear())); bb->addStretch(); // stretch = 0, so the height of the buttonbox is constant topLayout->addWidget(bb, 0, Qt::AlignBottom | Qt::AlignHCenter); m_save->setEnabled(false); // no currUnit exists, so disable the Copy and Delete button#ifdef SHOW_COPY_BTN m_copy->setEnabled(false);#endif m_delete->setEnabled(false);}void BCUnitEditWidget::slotReset() {// kdDebug() << "BCUnitEditWidget::slotReset()" << endl; m_currUnit = 0; m_save->setEnabled(false); m_delete->setEnabled(false); m_save->setText(i18n("Enter Book")); m_currColl = 0; //TODO might need this when support multiple collection types// clear all the dicts// m_editDict.clear();// m_multiDict.clear();// m_comboDict.clear();// m_checkDict.clear();}void BCUnitEditWidget::slotSetCollection(BCCollection* coll_) { if(!coll_) { return; }// kdDebug() << "BCUnitEditWidget::slotSetCollection() - " << coll_->title() << endl;// for now, reset, but if multiple collections are supported, this has to change slotHandleClear(); m_currColl = coll_; m_currUnit = new BCUnit(m_currColl);// don't do this, ,it would cause infinite looping// if(m_tabs->count() == 0) {// slotSetLayout(coll_);// } // go back to first tab, with title, etc... m_tabs->showTab(0);}void BCUnitEditWidget::slotSetLayout(BCCollection* coll_) { if(!coll_) { return; } // kdDebug() << "BCUnitEditWidget::slotSetLayout()" << endl; if(m_tabs->count() > 0) { kdDebug() << "BCUnitEditWidget::slotSetLayout() - tabs already exist." << endl; return; } slotSetCollection(coll_); KLineEdit* kl; QTextEdit* te; KComboBox* kc; QCheckBox* cb; QStringList catList = m_currColl->attributeCategories(); QStringList::ConstIterator catIt = catList.begin(); for( ; catIt != catList.end(); ++catIt) { QGrid* grid = new QGrid(NCOLS, m_tabs); grid->setMargin(10); grid->setSpacing(5); QString catName = static_cast<QString>(*catIt); BCAttributeList list = m_currColl->attributesByCategory(catName); BCAttributeListIterator it(list); for( ; it.current(); ++it) { QLabel* la = new QLabel(it.current()->title() + QString::fromLatin1(":"), grid); la->setAlignment(Qt::AlignRight | Qt::AlignVCenter); QWhatsThis::add(la, it.current()->description()); switch(it.current()->type()) { case BCAttribute::Line: kl = new KLineEdit(QString::null, grid); connect(kl, SIGNAL(textChanged(const QString&)), this, SLOT(slotSetModified())); QWhatsThis::add(kl, it.current()->description()); if(! (it.current()->flags() & BCAttribute::NoComplete)) { kl->completionObject()->setItems(m_currColl->valuesByAttributeName(it.current()->name())); kl->setAutoDeleteCompletionObject(true); } if(it.current()->name() == QString::fromLatin1("isbn")) { ISBNValidator* isbn = new ISBNValidator(this); kl->setValidator(isbn); } m_editDict.insert(QString::number(m_currColl->id()) + it.current()->name(), kl); break; case BCAttribute::Para: te = new QTextEdit(grid); te->setTextFormat(Qt::PlainText); connect(te, SIGNAL(textChanged()), this, SLOT(slotSetModified())); QWhatsThis::add(te, it.current()->description()); m_multiDict.insert(QString::number(m_currColl->id()) + it.current()->name(), te); break; case BCAttribute::Choice: kc = new KComboBox(grid); connect(kc, SIGNAL(activated(int)), this, SLOT(slotSetModified())); QWhatsThis::add(kc, it.current()->description()); // always have empty choice kc->insertItem(QString::null); kc->insertStringList(it.current()->allowed()); kc->setEditable(false); m_comboDict.insert(QString::number(m_currColl->id()) + it.current()->name(), kc); break; case BCAttribute::Bool: cb = new QCheckBox(grid); connect(cb, SIGNAL(clicked()), this, SLOT(slotSetModified())); QWhatsThis::add(cb, it.current()->description()); m_checkDict.insert(QString::number(m_currColl->id()) + it.current()->name(), cb); break; case BCAttribute::Year: kl = new KLineEdit(QString::null, grid); connect(kl, SIGNAL(textChanged(const QString&)), this, SLOT(slotSetModified())); QWhatsThis::add(kl, it.current()->description()); kl->setMaxLength(4); kl->setValidator(new QIntValidator(1000, 9999, this)); m_editDict.insert(QString::number(m_currColl->id()) + it.current()->name(), kl); break; case BCAttribute::ReadOnly: break; default: kdDebug() << "BCUnitEditWidget() - unknown attribute type (" << it.current()->type() << ") named " << it.current()->name() << endl; break; } // end switch } // I don't want anything to be hidden grid->setMinimumHeight(grid->sizeHint().height()); m_tabs->addTab(grid, catName); }// this doesn't seem to work// setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum));// so do this instead setMinimumHeight(sizeHint().height()); setMaximumHeight(sizeHint().height());}void BCUnitEditWidget::slotHandleNew() { if(!queryModified()) { return; } slotHandleClear(); m_currUnit = new BCUnit(m_currColl);// m_save->setText(i18n("Enter Book"));}void BCUnitEditWidget::slotHandleCopy() { // if the currUnit exists and has already been saved // TODO: if the attribute values have been changed without clicking save // need to ask user for confirmation if(m_currUnit && m_currColl && m_currColl->unitList().containsRef(m_currUnit) > 0) { m_currUnit = new BCUnit(*m_currUnit); // slotHandleSave() clears everthing, so need to keep a pointer BCUnit* unit = m_currUnit; slotHandleSave(); // let's be nice and put everything back in there slotSetContents(unit); }}void BCUnitEditWidget::slotHandleSave() { if(!m_currColl) { // big problem kdDebug() << "BCUnitEditWidget::slotHandleSave() - no valid collection pointer" << endl; } // make sure we have a good BCUnit pointer if(!m_currUnit) { m_currUnit = new BCUnit(m_currColl); kdDebug() << "BCUnitEditWidget::slotHandleSave() - new BCUnit pointer created" " in collection " << m_currColl->title() << endl; } // boolean to keep track if every possible attribute is empty bool empty = true; KLineEdit* kl; QTextEdit* te; KComboBox* kc; QCheckBox* cb; QString temp; BCAttributeListIterator it(m_currColl->attributeList()); for( ; it.current(); ++it) { switch(it.current()->type()) { case BCAttribute::Line: kl = m_editDict.find(QString::number(m_currColl->id()) + it.current()->name()); if(kl) { temp = kl->text().simplifyWhiteSpace(); // ok to set attribute empty string m_currUnit->setAttribute(it.current()->name(), temp); if(!temp.isEmpty()) { empty = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -