File indexing completed on 2024-03-24 15:47:59

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2016 Jonathan Marten <jjm@keelhaul.me.uk>     *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #include "dialogbase.h"
0032 
0033 #include <qlayout.h>
0034 #include <qframe.h>
0035 #include <qstyle.h>
0036 #include <qpushbutton.h>
0037 #include <qapplication.h>
0038 #include <QSpacerItem>
0039 
0040 #include <kguiitem.h>
0041 
0042 #include "dialogstatewatcher.h"
0043 #include "libdialogutil_logging.h"
0044 
0045 
0046 DialogBase::DialogBase(QWidget *pnt)
0047     : QDialog(pnt)
0048 {
0049     qCDebug(LIBDIALOGUTIL_LOG);
0050 
0051     setModal(true);                 // convenience, can reset if necessary
0052 
0053     mMainWidget = nullptr;                  // caller not provided yet
0054     mStateWatcher = new DialogStateWatcher(this);   // use our own as default
0055 
0056     mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this);
0057     connect(mButtonBox, &QDialogButtonBox::accepted, this, &DialogBase::accept);
0058     connect(mButtonBox, &QDialogButtonBox::rejected, this, &DialogBase::reject);
0059 }
0060 
0061 
0062 void DialogBase::showEvent(QShowEvent *ev)
0063 {
0064     if (layout()==nullptr)                  // layout not yet set up
0065     {
0066         qCDebug(LIBDIALOGUTIL_LOG) << "setup layout";
0067         QVBoxLayout *mainLayout = new QVBoxLayout;
0068         setLayout(mainLayout);
0069 
0070         if (mMainWidget==nullptr)
0071         {
0072             qCWarning(LIBDIALOGUTIL_LOG) << "No main widget set for" << objectName();
0073             mMainWidget = new QWidget(this);
0074         }
0075 
0076         mainLayout->addWidget(mMainWidget);
0077         mainLayout->setStretchFactor(mMainWidget, 1);
0078         mainLayout->addWidget(mButtonBox);
0079     }
0080 
0081     QDialog::showEvent(ev);             // show the dialogue
0082 }
0083 
0084 
0085 void DialogBase::setButtons(QDialogButtonBox::StandardButtons buttons)
0086 {
0087     qCDebug(LIBDIALOGUTIL_LOG) << buttons;
0088     mButtonBox->setStandardButtons(buttons);
0089 
0090     if (buttons & QDialogButtonBox::Ok)
0091     {
0092         qCDebug(LIBDIALOGUTIL_LOG) << "setup OK button";
0093         QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
0094         okButton->setDefault(true);
0095         okButton->setShortcut(Qt::CTRL|Qt::Key_Return);
0096     }
0097 
0098 // set F1 shortcut for Help?
0099 
0100 }
0101 
0102 
0103 void DialogBase::setButtonEnabled(QDialogButtonBox::StandardButton button, bool state)
0104 {
0105     QPushButton *but = mButtonBox->button(button);
0106     if (but!=nullptr) but->setEnabled(state);
0107 }
0108 
0109 
0110 void DialogBase::setButtonText(QDialogButtonBox::StandardButton button, const QString &text)
0111 {
0112     QPushButton *but = mButtonBox->button(button);
0113     if (but!=nullptr) but->setText(text);
0114 }
0115 
0116 void DialogBase::setButtonIcon(QDialogButtonBox::StandardButton button, const QIcon &icon)
0117 {
0118     QPushButton *but = mButtonBox->button(button);
0119     if (but!=nullptr) but->setIcon(icon);
0120 }
0121 
0122 void DialogBase::setButtonGuiItem(QDialogButtonBox::StandardButton button, const KGuiItem &guiItem)
0123 {
0124     QPushButton *but = mButtonBox->button(button);
0125     if (but!=nullptr) KGuiItem::assign(but, guiItem);
0126 }
0127 
0128 int DialogBase::verticalSpacing()
0129 {
0130     int spacing = QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing);
0131     return (spacing);
0132 }
0133 
0134 
0135 int DialogBase::horizontalSpacing()
0136 {
0137     int spacing = QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
0138     return (spacing);
0139 }
0140 
0141 
0142 void DialogBase::setStateSaver(DialogStateSaver *saver)
0143 {
0144     mStateWatcher->setStateSaver(saver);
0145 }
0146 
0147 
0148 DialogStateSaver *DialogBase::stateSaver() const
0149 {
0150     return (mStateWatcher->stateSaver());
0151 }
0152 
0153 
0154 QSpacerItem *DialogBase::verticalSpacerItem()
0155 {
0156     return (new QSpacerItem(1, verticalSpacing(), QSizePolicy::Minimum, QSizePolicy::Fixed));
0157 }
0158 
0159 
0160 QSpacerItem *DialogBase::horizontalSpacerItem()
0161 {
0162     return (new QSpacerItem(horizontalSpacing(), 1, QSizePolicy::Fixed, QSizePolicy::Minimum));
0163 }