File indexing completed on 2024-04-21 15:02:59

0001 /***************************************************************************
0002  * form.h
0003  * This file is part of the KDE project
0004  * copyright (C)2006-2007 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #ifndef KROSS_FORM_H
0021 #define KROSS_FORM_H
0022 
0023 #include <QWidget>
0024 #include <QUrl>
0025 #include <QListWidget>
0026 
0027 #include <kpagedialog.h>
0028 #include <kassistantdialog.h>
0029 //#include <kfilewidget.h>
0030 
0031 class QAbstractButton;
0032 
0033 namespace Kross
0034 {
0035 
0036 /**
0037  * The FormListView class provides access to a ListView.
0038  */
0039 class FormListView : public QListWidget
0040 {
0041     Q_OBJECT
0042 public:
0043     explicit FormListView(QWidget *parent);
0044     ~FormListView() override;
0045 public Q_SLOTS:
0046     void clear();
0047     void remove(int index);
0048     void addItem(const QString &text);
0049     int count();
0050     int current();
0051     void setCurrent(int row);
0052     QString text(int row);
0053 };
0054 
0055 /**
0056  * The FormFileWidget class provides access to a KFileWidget.
0057  */
0058 class FormFileWidget : public QWidget
0059 {
0060     Q_OBJECT
0061     Q_ENUMS(Mode)
0062 
0063 public:
0064     FormFileWidget(QWidget *parent, const QString &startDirOrVariable);
0065     ~FormFileWidget() override;
0066 
0067     /**
0068      * The Mode the FormFileWidget could have.
0069      */
0070     enum Mode { Other = 0, Opening, Saving };
0071 
0072 public Q_SLOTS:
0073 
0074     /**
0075      * Set the \a Mode the FormFileWidget should have to \p mode .
0076      * Valid modes are "Other", "Opening" or "Saving".
0077      */
0078     void setMode(const QString &mode);
0079 
0080     /**
0081      * \return the current filter.
0082      */
0083     QString currentFilter() const;
0084 
0085     /**
0086      * Set the filter to \p filter .
0087      */
0088     void setFilter(const QString &filter);
0089 
0090     /**
0091      * \return the current mimetype filter.
0092      */
0093     QString currentMimeFilter() const;
0094 
0095     /**
0096      * Set the mimetype filter to \p filter .
0097      */
0098     void setMimeFilter(const QStringList &filter);
0099 
0100     /**
0101      * \return the currently selected file.
0102      */
0103     QString selectedFile() const;
0104 
0105     //QStringList selectedFiles() const { return KFileDialog::selectedFiles(); }
0106     //QString selectedUrl() const { return KFileDialog::selectedUrl().toLocalFile(); }
0107 
0108 Q_SIGNALS:
0109 
0110     /**
0111     * Emitted when the user selects a file. It is only emitted in single-
0112     * selection mode.
0113     */
0114     void fileSelected(const QString &file);
0115 
0116     /**
0117     * Emitted when the user highlights a file.
0118     */
0119     void fileHighlighted(const QString &);
0120 
0121     /**
0122     * Emitted when the user highlights one or more files in multiselection mode.
0123     */
0124     void selectionChanged();
0125 
0126     /**
0127     * Emitted when the filter changed, i.e. the user entered an own filter
0128     * or chose one of the predefined set via setFilter().
0129     */
0130     void filterChanged(const QString &filter);
0131 
0132 private Q_SLOTS:
0133     void slotFileSelected(const QUrl &);
0134     void slotFileHighlighted(const QUrl &);
0135 
0136 private:
0137     /// \internal d-pointer class.
0138     class Private;
0139     /// \internal d-pointer instance.
0140     Private *const d;
0141 };
0142 
0143 /**
0144  * The FormProgressDialog class provides access to progressbar.
0145  *
0146  * Example (in Python) :
0147  * \code
0148  * import time, Kross
0149  * forms = Kross.module("forms")
0150  * progress = forms.showProgressDialog("My Title")
0151  * progress.setText("Some <i>detailed</i> text.")
0152  * for i in range(0,101):
0153  *     progress.setValue(i)
0154  *     progress.addText("%s" % i)
0155  *     time.sleep(1)
0156  * progress.reset()
0157  * \endcode
0158  */
0159 class FormProgressDialog : public KPageDialog
0160 {
0161     Q_OBJECT
0162 public:
0163     FormProgressDialog(const QString &caption, const QString &labelText);
0164     ~FormProgressDialog() override;
0165     void done(int r) override;
0166 public Q_SLOTS:
0167     /**
0168     * Set the value of the progressbar. If -1 the progressbar will be hidden.
0169     */
0170     void setValue(int progress);
0171     /**
0172     * Set the minimum and maximum range of the progressbar.
0173     */
0174     void setRange(int minimum, int maximum);
0175     /**
0176     * Set the HTML-text that is displayed as information to the text \p text .
0177     */
0178     void setText(const QString &text);
0179     /**
0180     * Add to the HTML-text that is displayed as information the text \p text .
0181     */
0182     void addText(const QString &text);
0183     /**
0184      * Shows the dialog as a modal dialog, blocking until the user
0185      * closes it and returns the execution result.
0186      *
0187      * \return >=1 if the dialog was accepted (e.g. "Ok" pressed) else
0188      * the user rejected the dialog (e.g. by pressing "Cancel" or just
0189      * closing the dialog by pressing the escape-key).
0190      */
0191     int exec() override;
0192     /**
0193      * Same as the \a exec() method above provided for Python-lovers (python
0194      * does not like functions named "exec" and PyQt named it "exec_loop", so
0195      * just let's do the same).
0196      */
0197     int exec_loop()
0198     {
0199         return exec();
0200     }
0201     /**
0202      * Returns true if the user requested to cancel the operation.
0203      */
0204     bool isCanceled();
0205 Q_SIGNALS:
0206     /**
0207      * This signal got emitted if the user requests to cancel the operation.
0208      */
0209     void canceled();
0210 private:
0211     /// \internal d-pointer class.
0212     class Private;
0213     /// \internal d-pointer instance.
0214     Private *const d;
0215 };
0216 
0217 /**
0218  * The FormDialog class provides access to KPageDialog objects as
0219  * top-level containers.
0220  *
0221  * Example (in Python) :
0222  * \code
0223  * import Kross
0224  * import sys,os
0225  * ourPath=(filter(lambda p: os.path.exists(p+'/mywidget.ui'),sys.path)+[''])[0]
0226  * forms = Kross.module("forms")
0227  * mydialog = forms.createDialog("MyDialog")
0228  * mydialog.setButtons("Ok|Cancel")
0229  * mydialog.setFaceType("Plain") #Auto Plain List Tree Tabbed
0230  * mypage = mydialog.addPage("name","header")
0231  * mywidget = forms.createWidgetFromUIFile(mypage, ourPath+'/mywidget.ui')
0232  * mywidget["lineEdit"].setText("some string")
0233  * if mydialog.exec_loop():
0234  *     if mydialog.result() == "Ok":
0235  *         print mywidget["lineEdit"].text
0236  * mydialog.deleteLater()
0237  * \endcode
0238  */
0239 class FormDialog: public KPageDialog
0240 {
0241     Q_OBJECT
0242 
0243 public:
0244     FormDialog(const QString &caption);
0245     ~FormDialog() override;
0246 
0247 public Q_SLOTS:
0248 
0249     /**
0250      * Set the buttons.
0251      *
0252      * \param buttons string that defines the displayed buttons. For example the
0253      * string may look like "Ok" or "Ok|Cancel" or "Yes|No|Cancel".
0254      * \return true if the passed \p buttons string was valid and setting the
0255      * buttons was successfully else false is returned.
0256      */
0257     bool setButtons(const QString &buttons);
0258 
0259     /**
0260      * Set the text of a button.
0261      *
0262      * Sample how to change the buttons of a dialog;
0263      * \code
0264      * dialog.setButtons("Yes|No|Cancel")
0265      * dialog.setButtonText("Yes","Overwrite")
0266      * dialog.setButtonText("No","Skip")
0267      * dialog.setButtonText("Cancel","Abort")
0268      * \endcode
0269      *
0270      * \param button string that defines the button that should be changed.
0271      * \param text string that should be used as button text.
0272      * \return true if the passed \p buttons string was valid and setting the
0273      * button text was successfully else false is returned.
0274      */
0275     bool setButtonText(const QString &button, const QString &text);
0276 
0277     /**
0278      * Set the face type of the dialog.
0279      *
0280      * \param facetype the face type which could be "Auto", "Plain", "List",
0281      * "Tree" or "Tabbed" as defined in \a KPageView::FaceType .
0282      */
0283     bool setFaceType(const QString &facetype);
0284 
0285     /**
0286      * \return the name of the currently selected page. Use the \a page()
0287      * method to get the matching page QWidget instance.
0288      */
0289     QString currentPage() const;
0290 
0291     /**
0292      * Set the current page to \p name . If there exists no page with
0293      * such a pagename the method returns false else (if the page was
0294      * successfully set) true is returned.
0295      */
0296     bool setCurrentPage(const QString &name);
0297 
0298     /**
0299      * \return the QWidget page instance which has the pagename \p name
0300      * or NULL if there exists no such page.
0301      */
0302     QWidget *page(const QString &name) const;
0303 
0304     /**
0305      * Add and return a new page.
0306      *
0307      * \param name The name the page has. This name is for example returned
0308      * at the \a currentPage() method and should be unique. The name is also
0309      * used to display a short title for the page.
0310      * \param header The longer header title text used for display purposes.
0311      * \param iconname The name of the icon which the page have. This could
0312      * be for example "about_kde", "document-open", "configure" or any other
0313      * iconname known by KDE.
0314      * \return the new QWidget page instance.
0315      */
0316     QWidget *addPage(const QString &name, const QString &header = QString(), const QString &iconname = QString());
0317 
0318     /**
0319      * Shows the dialog as a modal dialog, blocking until the user
0320      * closes it and returns the execution result.
0321      *
0322      * \return >=1 if the dialog was accepted (e.g. "Ok" pressed) else
0323      * the user rejected the dialog (e.g. by pressing "Cancel" or just
0324      * closing the dialog by pressing the escape-key).
0325      */
0326     int exec() override
0327     {
0328         return QDialog::exec();
0329     }
0330 
0331     /**
0332      * Same as the \a exec() method above provided for Python-lovers (python
0333      * does not like functions named "exec" and PyQt named it "exec_loop", so
0334      * just let's do the same).
0335      */
0336     int exec_loop()
0337     {
0338         return exec();
0339     }
0340 
0341     /**
0342      * \return the result. The result may for example "Ok", "Cancel", "Yes" or "No".
0343      */
0344     QString result();
0345 
0346 private Q_SLOTS:
0347     virtual void slotButtonClicked(QAbstractButton *button);
0348     void slotCurrentPageChanged(KPageWidgetItem *current);
0349 
0350 private:
0351     /// \internal d-pointer class.
0352     class Private;
0353     /// \internal d-pointer instance.
0354     Private *const d;
0355 };
0356 
0357 /**
0358  * The FormAssistant class provides access to KAssistantDialog objects as
0359  * top-level containers.
0360  *
0361  * Example (in Python) :
0362  * \code
0363  * import Kross
0364  * import sys,os
0365  * ourPath=(filter(lambda p: os.path.exists(p+'/mywidget.ui'),sys.path)+[''])[0]
0366  * forms = Kross.module("forms")
0367  * myassistant = forms.createAssistant("MyAssistant")
0368  * myassistant.showHelpButton(0)
0369  * mypage = myassistant.addPage("name","header")
0370  * mywidget = forms.createWidgetFromUIFile(mypage, ourPath+'/mywidget.ui')
0371  * mypage2 = myassistant.addPage("name2","header2")
0372  * mywidget2 = forms.createWidgetFromUIFile(mypage2, ourPath+'/mywidget.ui')
0373  * mypage3 = myassistant.addPage("name3","header3")
0374  * mywidget3 = forms.createWidgetFromUIFile(mypage3, ourPath+'/mywidget.ui')
0375  * mywidget["lineEdit"].setText("some string")
0376  *
0377  * def nextClicked():
0378  *     myassistant.setAppropriate("name2",0)
0379  * def finished():
0380  *     ...
0381  *     myassistant.deleteLater() #remember to cleanup
0382  *
0383  * myassistant.connect("nextClicked()",nextClicked)
0384  * myassistant.connect("finished()",finished)
0385  * myassistant.show()
0386  *
0387  * \endcode
0388  */
0389 class FormAssistant: public KAssistantDialog
0390 {
0391     Q_OBJECT
0392     Q_ENUMS(AssistantButtonCode)
0393 public:
0394     enum AssistantButtonCode {
0395         None    = 0x00000000,
0396         Help    = 0x00000001,
0397         Default = 0x00000002,
0398         Cancel  = 0x00000020,
0399         Finish  = 0x00001000,
0400         Next    = 0x00002000,
0401         Back    = 0x00004000,
0402         NoDefault = 0x00008000
0403     };
0404     Q_DECLARE_FLAGS(AssistantButtonCodes, AssistantButtonCode)
0405 
0406 public:
0407     FormAssistant(const QString &caption);
0408     ~FormAssistant() override;
0409 
0410 public Q_SLOTS:
0411 
0412     void showHelpButton(bool);
0413 
0414     /**
0415      * \return the name of the currently selected page. Use the \a page()
0416      * method to get the matching page QWidget instance.
0417      */
0418     QString currentPage() const;
0419 
0420     /**
0421      * Set the current page to \p name . If there exists no page with
0422      * such a pagename the method returns false else (if the page was
0423      * successfully set) true is returned.
0424      */
0425     bool setCurrentPage(const QString &name);
0426 
0427     /**
0428      * \return the QWidget page instance which has the pagename \p name
0429      * or NULL if there exists no such page.
0430      */
0431     QWidget *page(const QString &name) const;
0432 
0433     /**
0434      * Add and return a new page.
0435      *
0436      * \param name The name the page has. This name is for example returned
0437      * at the \a currentPage() method and should be unique. The name is also
0438      * used to display a short title for the page.
0439      * \param header The longer header title text used for display purposes.
0440      * \param iconname The name of the icon which the page have. This could
0441      * be for example "about_kde", "document-open", "configure" or any other
0442      * iconname known by KDE.
0443      * \return the new QWidget page instance.
0444      */
0445     QWidget *addPage(const QString &name, const QString &header = QString(), const QString &iconname = QString());
0446 
0447     /**
0448      * @see KAssistantDialog::isAppropriate()
0449      */
0450     bool isAppropriate(const QString &name) const;
0451     /**
0452      * @see KAssistantDialog::setAppropriate()
0453      */
0454     void setAppropriate(const QString &name, bool appropriate);
0455     /**
0456      * @see KAssistantDialog::isValid()
0457      */
0458     bool isValid(const QString &name) const;
0459     /**
0460      * @see KAssistantDialog::setValid()
0461      */
0462     void setValid(const QString &name, bool enable);
0463 
0464     /**
0465      * Shows the dialog as a modal dialog, blocking until the user
0466      * closes it and returns the execution result.
0467      *
0468      * \return >=1 if the dialog was accepted (e.g. "Finished" pressed) else
0469      * the user rejected the dialog (e.g. by pressing "Cancel" or just
0470      * closing the dialog by pressing the escape-key).
0471      */
0472     int exec() override
0473     {
0474         return QDialog::exec();
0475     }
0476 
0477     /**
0478      * Same as the \a exec() method above provided for Python-lovers (python
0479      * does not like functions named "exec" and PyQt named it "exec_loop", so
0480      * just let's do the same).
0481      */
0482     int exec_loop()
0483     {
0484         return exec();
0485     }
0486 
0487     /**
0488      * \return the result. The result may for example "Finish" or "Cancel".
0489      */
0490     QString result();
0491 
0492     /**
0493      * Force page switching. This will also emit backClicked()
0494      */
0495     void back() override;
0496     /**
0497      * Force page switching. This will also emit nextClicked()
0498      */
0499     void next() override;
0500 
0501 private Q_SLOTS:
0502     virtual void slotButtonClicked(QAbstractButton *button);
0503     void slotCurrentPageChanged(KPageWidgetItem *current);
0504 
0505 Q_SIGNALS:
0506     /**
0507      * use it to setAppropriate()
0508      */
0509     void nextClicked();
0510     void backClicked();
0511 
0512 private:
0513     /// \internal d-pointer class.
0514     class Private;
0515     /// \internal d-pointer instance.
0516     Private *const d;
0517 };
0518 
0519 /**
0520  * The FormModule provides access to UI functionality like dialogs or widgets.
0521  *
0522  * Example (in Python) :
0523  * \code
0524  * import Kross
0525  * forms = Kross.module("forms")
0526  * dialog = forms.createDialog("My Dialog")
0527  * dialog.setButtons("Ok|Cancel")
0528  * page = dialog.addPage("Welcome","Welcome Page","document-open")
0529  * label = forms.createWidget(page,"QLabel")
0530  * label.text = "Hello World Label"
0531  * if dialog.exec_loop():
0532  *     forms.showMessageBox("Information", "Okay...", "The Ok-button was pressed")
0533  * \endcode
0534  */
0535 class FormModule: public QObject
0536 {
0537     Q_OBJECT
0538 
0539 public:
0540     explicit FormModule();
0541     ~FormModule() override;
0542 
0543 public Q_SLOTS:
0544 
0545     /**
0546      * \return the active modal widget. Modal widgets are special top-level
0547      * widgets which are subclasses of QDialog and are modal.
0548      */
0549     QWidget *activeModalWidget();
0550 
0551     /**
0552      * \return the application top-level window that has the keyboard input
0553      * focus, or NULL if no application window has the focus.
0554      */
0555     QWidget *activeWindow();
0556 
0557     /**
0558      * \return i18n'ed version of the string
0559      */
0560     QString tr(const QString &str);
0561 
0562     /**
0563      * \return i18n'ed version of the string, differentiated by the comment text (like '\@title:window')
0564      */
0565     QString tr(const QString &str, const QString &comment);
0566 
0567     /**
0568      * Show a messagebox.
0569      *
0570      * \param dialogtype The type of the dialog which could be one
0571      * of the following;
0572      *      \li Sorry
0573      *      \li Error
0574      *      \li Information
0575      *      \li QuestionYesNo
0576      *      \li WarningYesNo
0577      *      \li WarningContinueCancel
0578      *      \li WarningYesNoCancel
0579      *      \li QuestionYesNoCancel
0580      * \param caption The caption the messagedialog displays.
0581      * \param message The message that is displayed in the messagedialog.
0582      * \param details The optional details
0583      * \return The buttoncode which could be one of the following;
0584      *      \li Ok
0585      *      \li Cancel
0586      *      \li Yes
0587      *      \li No
0588      *      \li Continue
0589      */
0590     QString showMessageBox(const QString &dialogtype, const QString &caption, const QString &message, const QString &details = QString());
0591 
0592     /**
0593      * Show a progressdialog to provide visible feedback on the progress
0594      * of a slow operation.
0595      *
0596      * \param caption The caption the progressdialog displays.
0597      * \param labelText The displayed label.
0598      * \return The QProgressDialog widget instance.
0599      */
0600     QWidget *showProgressDialog(const QString &caption, const QString &labelText);
0601 
0602     /**
0603      * Create and return a new \a FormDialog instance.
0604      *
0605      * \param caption The displayed caption of the dialog.
0606      */
0607     QWidget *createDialog(const QString &caption);
0608 
0609     /**
0610      * Create and return a new \a FormAssistant instance.
0611      *
0612      * \param caption The displayed caption of the dialog.
0613      */
0614     QWidget *createAssistant(const QString &caption);
0615 
0616     /**
0617      * Create and return a new QWidget instance.
0618      *
0619      * \param parent the parent QWidget the new QWidget is a child of.
0620      * \param layout the layout style the widget has. This could be one
0621      * of the following strings;
0622      *        \li QVBoxLayout
0623      *        \li QHBoxLayout
0624      *        \li QStackedLayout
0625      * \return the new QLayout instance or NULL.
0626      */
0627     QObject *createLayout(QWidget *parent, const QString &layout);
0628 
0629     /**
0630      * Create and return a new QWidget instance.
0631      *
0632      * \param parent the parent QWidget the new QWidget is a child of.
0633      * \param className the name of the class that should be created
0634      * and returned. For example "QLabel" or "QForm".
0635      * \param name the objectName the new widget has.
0636      * \return the new QWidget instance or NULL.
0637      */
0638     QWidget *createWidget(const QString &className);
0639 
0640     /**
0641      * Create and return a new QWidget instance.
0642      *
0643      * \param parent the parent QWidget the new QWidget is a child of.
0644      * \param className the name of the class that should be created
0645      * and returned. For example "QLabel" or "QForm".
0646      * \param name the objectName the new widget has.
0647      * \return the new QWidget instance or NULL.
0648      */
0649     QWidget *createWidget(QWidget *parent, const QString &className, const QString &name = QString());
0650 
0651     /**
0652      * Create and return a new QWidget instance.
0653      *
0654      * \param parent the new QWidget is a child of parent.
0655      * \param xml the UI XML string used to construct the new widget.
0656      * \return the new QWidget instance or NULL.
0657      */
0658     QWidget *createWidgetFromUI(QWidget *parent, const QString &xml);
0659 
0660     /**
0661      * Create and return a new QWidget instance.
0662      *
0663      * \param parent the parent QWidget the new QWidget is a child of.
0664      * \param filename the full filename of the UI file which is read
0665      * and its UI XML content is used to construct the new widget.
0666      */
0667     QWidget *createWidgetFromUIFile(QWidget *parent, const QString &filename);
0668 
0669     /**
0670      * Create and return a new \a FormFileWidget instance.
0671      *
0672      * \param parent the parent QWidget the new \a FormFileWidget instance
0673      * is a child of.
0674      * \param startDirOrVariable the start-directory or -variable.
0675      * \return the new \a FormFileWidget instance or NULL.
0676      */
0677     QWidget *createFileWidget(QWidget *parent, const QString &startDirOrVariable = QString());
0678 
0679     /**
0680      * Create and return a new \a FormListView instance.
0681      *
0682      * \param parent the parent QWidget the new \a FormListView instance
0683      * is a child of.
0684      * \return the new \a FormFileWidget instance or NULL.
0685      */
0686     QWidget *createListView(QWidget *parent);
0687 
0688     /**
0689      * Load and return a KPart component.
0690      * \param parent The parent QWidget the KPart's widget will be child of.
0691      * \param name The name of the KPart library like e.g. "libkhtmlpart".
0692      * \param url Optional Url that should be opened on load.
0693      */
0694     QObject *loadPart(QWidget *parent, const QString &name, const QUrl &url = QUrl());
0695 
0696     /**
0697      * Create and return a new \a QAction instance.
0698      *
0699      * \param parent the parent QObject the new \a QAction instance
0700      * is a child of.
0701      * \return the new \a QAction instance or NULL.
0702      */
0703     QAction *createAction(QObject *parent);
0704 
0705 private:
0706     /// \internal d-pointer class.
0707     class Private;
0708     /// \internal d-pointer instance.
0709     Private *const d;
0710 };
0711 }
0712 
0713 #endif
0714