JKQtExtras
a library of useful Qt widgets and tools
jkqteprogresslistwidget.h
1 /*
2  Copyright (c) 2008-2020 Jan W. Krieger (<jan@jkrieger.de>)
3 
4  This software is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License (LGPL) as published by
6  the Free Software Foundation, either version 2.1 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License (LGPL) for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License (LGPL)
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef JKQTEPROGRESSLISTWIDGET_H
19 #define JKQTEPROGRESSLISTWIDGET_H
20 
21 #include <QWidget>
22 #include <QStringList>
23 #include <QMap>
24 #include <QIcon>
25 #include <QList>
26 #include "jkqtextras_imexport.h"
27 #include <QLabel>
28 #include <QGridLayout>
29 #include <QPushButton>
30 #include <QDialog>
31 #include <QProgressBar>
32 
33 
34 /*! \brief displays a list of items with an icon next to each which indicates the current task performed on the item (e.g. not started, in progress, success, failed, ...)
35  \ingroup JKQtExtrasWidgetsProgress
36 
37  \image html JKQTEProgressListWidget.png
38 
39  Usage example:
40 
41  \snippet jkqtextras_test/testmainwindow.cpp Example: JKQTEProgressListWidget
42 
43  There are two types of APIs that allow to modify the items:
44  - You can use setItemStatus() to explicitly set a status for an item
45  - you can use start() to set the first item in state JKQTEProgressListWidget::statusNotStarted
46  to go to JKQTEProgressListWidget::statusInProgress. Then you can call nextItem(newState)
47  to set the status of the item currently in JKQTEProgressListWidget::statusInProgress to the
48  give \c newState and set the next item to JKQTEProgressListWidget::statusInProgress .
49  .
50  */
52  Q_OBJECT
53  public:
54  enum {
55  statusNone=0,
56  statusNotStarted=1,
57  statusDone=2,
58  statusFailed=3,
59  statusInProgress=4,
60  statusUser=5
61  };
62 
63  explicit JKQTEProgressListWidget(QWidget *parent = nullptr);
64 
65  /** \brief define an icon to be used for a given status.
66  *
67  * \note The widget pre-defines default icons for the states JKQTEProgressListWidget::statusNone, JKQTEProgressListWidget::statusNotStarted, JKQTEProgressListWidget::statusDone, JKQTEProgressListWidget::statusFailed, JKQTEProgressListWidget::statusInProgress. */
68  void defineIcon(int status, QIcon icon);
69  /** \brief returns the icon used for a given \a status */
70  QIcon getIcon(int status) const;
71 
72  /** \brief returns the status of a given \a item */
73  int getStatus(int item) const;
74  /** \brief returns the text of a given \a item */
75  QString getText(int item) const;
76  /** \brief deletes a given \a item */
77  void deleteItem(int item);
78  /** \brief returns the number of items in the list */
79  int count() const;
80  /** \brief add an item to the list */
81  void addItem(const QString& text, int status=statusNotStarted);
82  /** \brief returns currently used icon size for status icons */
83  QSize getIconSize() const;
84  /** \brief inidctaes whether to center the display */
85  bool isCentered() const;
86 
87  public slots:
88  /** \brief searches for the first item with status statusInProgress and switches it's status to \a newStatusLast The next item with status statusNotStarted is switched to statusInProgress */
89  void nextItem(int newStatusLast=statusDone);
90  /** \brief searched for the first item with status statusNotStarted and sets it to statusInProgress */
91  void start();
92  /** \brief reset the list, i.e. set all items to JKQTEProgressListWidget::statusNotStarted */
93  void reset();
94 
95  /** \brief set the status of the \a item -th item (allowed values: JKQTEProgressListWidget::statusNone, JKQTEProgressListWidget::statusNotStarted, JKQTEProgressListWidget::statusDone, JKQTEProgressListWidget::statusFailed, JKQTEProgressListWidget::statusInProgress, JKQTEProgressListWidget::statusUser ...) */
96  void setItemStatus(int item, int status);
97  /** \brief set the text of the \a item -th item */
98  void setItemText(int item, const QString& text);
99 
100  /** \brief inidctaes whether to center the display */
101  void setCentered(bool centered);
102  /** \brief sets the currently used icon size for status icons */
103  void setIconSize(QSize size);
104  /** \brief sets the currently used icon size for status icons */
105  void setIconSize(int size);
106 
107  protected:
108  struct widgetstruct {
109  QLabel* labIcon;
110  QLabel* labText;
111  };
112 
114 
115  QStringList m_items;
116  QList<int> m_status;
117  QMap<int, QIcon> m_icons;
118  QList<widgetstruct> m_widgets;
119  QSize m_iconSize;
120  QGridLayout* layout;
121 
123 };
124 
125 
126 /** \brief a dialog with a JKQTEProgressListWidget and a cancel button (optional)
127  * \ingroup JKQtExtrasWidgetsProgress
128  *
129  * \image html JKQTEProgressListDialog.png
130  *
131  * Usage example:
132  *
133  * \snippet jkqtextras_test/testmainwindow.cpp Example: JKQTEProgressListDialog
134  *
135  * \see JKQTEProgressListWidget
136  */
138  Q_OBJECT
139  public:
140  JKQTEProgressListDialog(QWidget * parent = nullptr, Qt::WindowFlags f = nullptr);
141  JKQTEProgressListDialog(const QString & labelText, const QString & cancelButtonText, int minimum, int maximum, QWidget * parent = 0, Qt::WindowFlags f = 0);
143 
144  /** \brief switch visibility of cancel button (\a hasCancel \c =true ) and whether pressing it causes the dialog to be rejected. */
145  void setHasCancelButton(bool hasCancel, bool cancelRejects=false);
146  /** \brief switch additional progress indicator on or off */
147  void setHasProgressBar(bool hasProg=true);
148  /** \brief indicates whether the dialog was canceled (via the cancel button or calling cancel() ) */
149  bool wasCanceled() const;
150  /** \copydoc JKQTEProgressListWidget::defineIcon() */
151  void defineIcon(int status, QIcon icon);
152  /** \copydoc JKQTEProgressListWidget::addItem() */
153  void addItem(const QString& text, int status=JKQTEProgressListWidget::statusNotStarted);
154  /** \copydoc JKQTEProgressListWidget::count() */
155  int count() const;
156  public slots:
157  /** \brief cancel the dialog */
158  void cancel();
159  /** \brief searches for the first item with status statusInProgress and switches it's status to \a newStatusLast The next item with status statusNotStarted is switched to statusInProgress */
161  /** \copydoc JKQTEProgressListWidget::start() */
162  void start();
163  /** \copydoc JKQTEProgressListWidget::reset() */
164  void reset();
165 
166  /** \copydoc JKQTEProgressListWidget::setItemStatus() */
167  void setItemStatus(int item, int status);
168  /** \copydoc JKQTEProgressListWidget::setItemText() */
169  void setItemText(int item, const QString& text);
170  /** \brief set the range of the progress indicator */
171  void setRange(int min, int max);
172  /** \brief set the range minimum of the progress indicator */
173  void setMinimum(int min);
174  /** \brief set the range maximum of the progress indicator */
175  void setMaximum(int max);
176  /** \brief set the value of the progress indicator */
177  void setValue(int value);
178  /** \brief set the text on the progress indicator*/
179  void setProgressText(const QString& text);
180  /** \brief set the text on the label */
181  void setLabelText(const QString& text);
182  /** \brief set the text on the cancel button */
183  void setCancelButtonText(const QString& text);
184  signals:
185  /** \brief emitted when the dialog is canceled */
186  void canceled();
187  protected:
189  QPushButton* btnCancel;
193  QLabel* labProgress;
194  QProgressBar* progress;
195  QFrame* progressLine;
196 
197  virtual void showEvent(QShowEvent* event);
199 };
200 
201 
202 #endif // JKQTEPROGRESSLISTWIDGET_H
203 
JKQTEProgressListDialog::JKQTEProgressListDialog
JKQTEProgressListDialog(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
JKQTEProgressListDialog::m_wasCanceled
bool m_wasCanceled
Definition: jkqteprogresslistwidget.h:190
JKQTEProgressListWidget::m_widgets
QList< widgetstruct > m_widgets
Definition: jkqteprogresslistwidget.h:118
JKQTEProgressListWidget::m_iconSize
QSize m_iconSize
Definition: jkqteprogresslistwidget.h:119
JKQTEProgressListWidget::setIconSize
void setIconSize(QSize size)
sets the currently used icon size for status icons
JKQTEProgressListDialog::addItem
void addItem(const QString &text, int status=JKQTEProgressListWidget::statusNotStarted)
add an item to the list
JKQTEProgressListDialog::canceled
void canceled()
emitted when the dialog is canceled
JKQTEProgressListDialog::createWidgets
void createWidgets()
JKQTEProgressListDialog::setItemText
void setItemText(int item, const QString &text)
set the text of the item -th item
JKQTEProgressListWidget::m_centered
bool m_centered
Definition: jkqteprogresslistwidget.h:113
JKQTEProgressListWidget::getIconSize
QSize getIconSize() const
returns currently used icon size for status icons
JKQTEProgressListDialog::wasCanceled
bool wasCanceled() const
indicates whether the dialog was canceled (via the cancel button or calling cancel() )
JKQTEProgressListWidget::setItemText
void setItemText(int item, const QString &text)
set the text of the item -th item
JKQTEProgressListDialog::setValue
void setValue(int value)
set the value of the progress indicator
JKQTEProgressListDialog::getProgressList
JKQTEProgressListWidget * getProgressList() const
JKQTEProgressListDialog::setMinimum
void setMinimum(int min)
set the range minimum of the progress indicator
JKQTEProgressListWidget::statusNotStarted
@ statusNotStarted
Definition: jkqteprogresslistwidget.h:56
JKQTEProgressListDialog::defineIcon
void defineIcon(int status, QIcon icon)
define an icon to be used for a given status.
JKQTEProgressListDialog::setLabelText
void setLabelText(const QString &text)
set the text on the label
JKQTEProgressListDialog::setRange
void setRange(int min, int max)
set the range of the progress indicator
JKQTEProgressListDialog::JKQTEProgressListDialog
JKQTEProgressListDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent=0, Qt::WindowFlags f=0)
JKQTEProgressListDialog::list
JKQTEProgressListWidget * list
Definition: jkqteprogresslistwidget.h:188
JKQTEProgressListWidget::m_items
QStringList m_items
Definition: jkqteprogresslistwidget.h:115
JKQTEProgressListWidget::setIconSize
void setIconSize(int size)
sets the currently used icon size for status icons
JKQTEProgressListWidget::setCentered
void setCentered(bool centered)
inidctaes whether to center the display
JKQTEProgressListDialog::cancel
void cancel()
cancel the dialog
JKQTEXTRAS_LIB_EXPORT
#define JKQTEXTRAS_LIB_EXPORT
Definition: jkqtextras_imexport.h:95
JKQTEProgressListWidget::isCentered
bool isCentered() const
inidctaes whether to center the display
JKQTEProgressListWidget::nextItem
void nextItem(int newStatusLast=statusDone)
searches for the first item with status statusInProgress and switches it's status to newStatusLast Th...
JKQTEProgressListDialog::setCancelButtonText
void setCancelButtonText(const QString &text)
set the text on the cancel button
JKQTEProgressListWidget::widgetstruct::labIcon
QLabel * labIcon
Definition: jkqteprogresslistwidget.h:109
JKQTEProgressListWidget::start
void start()
searched for the first item with status statusNotStarted and sets it to statusInProgress
JKQTEProgressListDialog::setProgressText
void setProgressText(const QString &text)
set the text on the progress indicator
JKQTEProgressListDialog::setHasCancelButton
void setHasCancelButton(bool hasCancel, bool cancelRejects=false)
switch visibility of cancel button (hasCancel =true ) and whether pressing it causes the dialog to be...
JKQTEProgressListDialog::setMaximum
void setMaximum(int max)
set the range maximum of the progress indicator
JKQTEProgressListWidget::deleteItem
void deleteItem(int item)
deletes a given item
JKQTEProgressListDialog::showEvent
virtual void showEvent(QShowEvent *event)
JKQTEProgressListDialog::labProgress
QLabel * labProgress
Definition: jkqteprogresslistwidget.h:193
JKQTEProgressListWidget::getStatus
int getStatus(int item) const
returns the status of a given item
JKQTEProgressListDialog::start
void start()
searched for the first item with status statusNotStarted and sets it to statusInProgress
JKQTEProgressListDialog::count
int count() const
returns the number of items in the list
JKQTEProgressListWidget::addItem
void addItem(const QString &text, int status=statusNotStarted)
add an item to the list
JKQTEProgressListWidget::layout
QGridLayout * layout
Definition: jkqteprogresslistwidget.h:120
JKQTEProgressListDialog::progressLine
QFrame * progressLine
Definition: jkqteprogresslistwidget.h:195
JKQTEProgressListWidget::m_icons
QMap< int, QIcon > m_icons
Definition: jkqteprogresslistwidget.h:117
JKQTEProgressListDialog::progress
QProgressBar * progress
Definition: jkqteprogresslistwidget.h:194
JKQTEProgressListDialog::nextItem
void nextItem(int newStatusLast=JKQTEProgressListWidget::statusDone)
searches for the first item with status statusInProgress and switches it's status to newStatusLast Th...
JKQTEProgressListWidget::getIcon
QIcon getIcon(int status) const
returns the icon used for a given status
JKQTEProgressListDialog::setHasProgressBar
void setHasProgressBar(bool hasProg=true)
switch additional progress indicator on or off
JKQTEProgressListDialog::setItemStatus
void setItemStatus(int item, int status)
set the status of the item -th item (allowed values: JKQTEProgressListWidget::statusNone,...
JKQTEProgressListDialog::btnCancel
QPushButton * btnCancel
Definition: jkqteprogresslistwidget.h:189
JKQTEProgressListWidget::reset
void reset()
reset the list, i.e. set all items to JKQTEProgressListWidget::statusNotStarted
JKQTEProgressListWidget::m_status
QList< int > m_status
Definition: jkqteprogresslistwidget.h:116
JKQTEProgressListWidget::JKQTEProgressListWidget
JKQTEProgressListWidget(QWidget *parent=nullptr)
JKQTEProgressListWidget
displays a list of items with an icon next to each which indicates the current task performed on the ...
Definition: jkqteprogresslistwidget.h:51
JKQTEProgressListWidget::widgetstruct
Definition: jkqteprogresslistwidget.h:108
JKQTEProgressListDialog::m_hasProgressBar
bool m_hasProgressBar
Definition: jkqteprogresslistwidget.h:192
JKQTEProgressListWidget::updateWidgets
void updateWidgets()
JKQTEProgressListDialog::reset
void reset()
reset the list, i.e. set all items to JKQTEProgressListWidget::statusNotStarted
JKQTEProgressListDialog::m_cancelRejects
bool m_cancelRejects
Definition: jkqteprogresslistwidget.h:191
JKQTEProgressListWidget::defineIcon
void defineIcon(int status, QIcon icon)
define an icon to be used for a given status.
JKQTEProgressListWidget::count
int count() const
returns the number of items in the list
JKQTEProgressListWidget::widgetstruct::labText
QLabel * labText
Definition: jkqteprogresslistwidget.h:110
JKQTEProgressListWidget::statusDone
@ statusDone
Definition: jkqteprogresslistwidget.h:57
JKQTEProgressListWidget::getText
QString getText(int item) const
returns the text of a given item
JKQTEProgressListDialog
a dialog with a JKQTEProgressListWidget and a cancel button (optional)
Definition: jkqteprogresslistwidget.h:137
JKQTEProgressListWidget::setItemStatus
void setItemStatus(int item, int status)
set the status of the item -th item (allowed values: JKQTEProgressListWidget::statusNone,...