libqutim
0.3.1.0
|
00001 /**************************************************************************** 00002 ** 00003 ** qutIM - instant messenger 00004 ** 00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru> 00006 ** 00007 ***************************************************************************** 00008 ** 00009 ** $QUTIM_BEGIN_LICENSE$ 00010 ** This program is free software: you can redistribute it and/or modify 00011 ** it under the terms of the GNU General Public License as published by 00012 ** the Free Software Foundation, either version 3 of the License, or 00013 ** (at your option) any later version. 00014 ** 00015 ** This program is distributed in the hope that it will be useful, 00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00018 ** See the GNU General Public License for more details. 00019 ** 00020 ** You should have received a copy of the GNU General Public License 00021 ** along with this program. If not, see http://www.gnu.org/licenses/. 00022 ** $QUTIM_END_LICENSE$ 00023 ** 00024 ****************************************************************************/ 00025 #ifndef DATAFORMS_H 00026 #define DATAFORMS_H 00027 00028 #include "localizedstring.h" 00029 #include <QSharedPointer> 00030 #include <QVariant> 00031 #include <QFrame> 00032 #include <QDateTime> 00033 #include <QDate> 00034 #include <QPixmap> 00035 #include <QImage> 00036 #include <QIcon> 00037 00038 class QValidator; 00039 00040 namespace qutim_sdk_0_3 00041 { 00042 00043 class DataItemPrivate; 00044 00216 class LIBQUTIM_EXPORT DataItem 00217 { 00218 public: 00223 DataItem(); 00227 DataItem(const QString &name, const LocalizedString &title, const QVariant &data); 00231 DataItem(const LocalizedString &title, const QVariant &data = QVariant()); 00239 DataItem(const DataItem &other); 00243 ~DataItem(); 00247 DataItem &operator=(const DataItem &other); 00252 QString name() const; 00257 void setName(const QString &itemName); 00262 LocalizedString title() const; 00267 void setTitle(const LocalizedString &itemTitle); 00271 QVariant data() const; 00276 template<typename T> 00277 T data(const T &def = T()) const; 00281 void setData(const QVariant &itemData); 00285 bool isNull() const; 00290 QList<DataItem> subitems() const; 00294 void setSubitems(const QList<DataItem> &newSubitems); 00298 DataItem subitem(const QString &name, bool recursive = false) const; 00299 00300 int removeSubitems(const QString &name, bool recursive = false); 00301 bool removeSubitem(const QString &name, bool recursive = false); 00302 DataItem takeSubitem(const QString &name, bool recursive = false); 00307 void addSubitem(const DataItem &subitem); 00311 bool hasSubitems() const; 00318 void allowModifySubitems(const DataItem &defaultSubitem, int maxSubitemsCount = -1); 00323 bool isAllowedModifySubitems() const; 00329 int maxSubitemsCount() const; 00334 DataItem defaultSubitem() const; 00339 bool isReadOnly() const; 00344 void setReadOnly(bool readOnly = true); 00353 void setDataChangedHandler(QObject *receiver, const char *method); 00357 QObject *dataChangedReceiver() const; 00361 const char *dataChangedMethod() const; 00366 DataItem &operator<<(const DataItem &subitem); 00372 QVariant property(const char *name, const QVariant &def = QVariant()) const; 00378 template<typename T> 00379 T property(const char *name, const T &def = T()) const 00380 { return qVariantValue<T>(property(name, qVariantFromValue<T>(def))); } 00385 void setProperty(const char *name, const QVariant &value); 00386 QList<QByteArray> dynamicPropertyNames() const; 00387 protected: 00388 #ifndef Q_QDOC 00389 friend class DataItemPrivate; 00390 QSharedDataPointer<DataItemPrivate> d; 00391 #endif 00392 }; 00393 00394 00395 class LIBQUTIM_EXPORT ReadOnlyDataItem : public DataItem 00396 { 00397 public: 00398 ReadOnlyDataItem(const LocalizedString &title, const QStringList &data); 00399 ReadOnlyDataItem(const LocalizedString &title, const LocalizedStringList &data); 00400 ReadOnlyDataItem(const LocalizedString &title, bool data); 00401 ReadOnlyDataItem(const LocalizedString &title, const QDate &data); 00402 ReadOnlyDataItem(const LocalizedString &title, const QDateTime &data); 00403 ReadOnlyDataItem(const LocalizedString &title, int data); 00404 ReadOnlyDataItem(const LocalizedString &title, uint data); 00405 ReadOnlyDataItem(const LocalizedString &title, double data); 00406 ReadOnlyDataItem(const LocalizedString &title, const QIcon &data); 00407 ReadOnlyDataItem(const LocalizedString &title, const QImage &data); 00408 ReadOnlyDataItem(const LocalizedString &title, const QPixmap &data); 00409 ReadOnlyDataItem(const LocalizedString &title, const QString &data); 00410 ReadOnlyDataItem(const LocalizedString &title, const LocalizedString &data); 00411 }; 00412 00413 class LIBQUTIM_EXPORT StringListDataItem : public DataItem 00414 { 00415 public: 00416 StringListDataItem(const QString &name, const LocalizedString &title, 00417 const QStringList &data = QStringList(), 00418 int maxStringsCount = -1); 00419 StringListDataItem(const QString &name, const LocalizedString &title, 00420 const LocalizedStringList &data = LocalizedStringList(), 00421 int maxStringsCount = -1); 00422 }; 00423 00424 class LIBQUTIM_EXPORT IntDataItem : public DataItem 00425 { 00426 public: 00427 IntDataItem(const QString &name, const LocalizedString &title, int data, 00428 int minValue = 0, int maxValue = 100); 00429 }; 00430 00431 class LIBQUTIM_EXPORT DoubleDataItem : public DataItem 00432 { 00433 public: 00434 DoubleDataItem(const QString &name, const LocalizedString &title, double data, 00435 double minValue = 0, double maxValue = 100); 00436 }; 00437 00438 class LIBQUTIM_EXPORT IconDataItem : public DataItem 00439 { 00440 public: 00441 IconDataItem(const QString &name, const LocalizedString &title, 00442 const QIcon &data = QIcon(), 00443 const QSize &imageSize = QSize(32, 32), 00444 const QList<QIcon> &alternatives = QList<QIcon>()); 00445 }; 00446 00447 class LIBQUTIM_EXPORT ImageDataItem : public DataItem 00448 { 00449 public: 00450 ImageDataItem(const QString &name, const LocalizedString &title, 00451 const QImage &data = QImage(), 00452 const QSize &imageSize = QSize(32, 32), 00453 const QList<QImage> &alternatives = QList<QImage>()); 00454 }; 00455 00456 class LIBQUTIM_EXPORT PixmapDataItem : public DataItem 00457 { 00458 public: 00459 PixmapDataItem(const QString &name, const LocalizedString &title, 00460 const QPixmap &data = QPixmap(), 00461 const QSize &imageSize = QSize(128, 128), 00462 const QList<QPixmap> &alternatives = QList<QPixmap>()); 00463 }; 00464 00465 class LIBQUTIM_EXPORT StringChooserDataItem : public DataItem 00466 { 00467 public: 00468 StringChooserDataItem(const QString &name, const LocalizedString &title, 00469 const QStringList &alternatives, const QString &data = QString(), 00470 bool editable = false, QValidator *validator = 0); 00471 StringChooserDataItem(const QString &name, const LocalizedString &title, 00472 const QStringList &alternatives, const QString &data, 00473 bool editable, QRegExp validator); 00474 StringChooserDataItem(const QString &name, const LocalizedString &title, 00475 const LocalizedStringList &alternatives, 00476 const LocalizedString &data, 00477 bool editable = false, QValidator *validator = 0); 00478 StringChooserDataItem(const QString &name, const LocalizedString &title, 00479 const LocalizedStringList &alternatives, 00480 const LocalizedString &data, 00481 bool editable, QRegExp validator); 00482 }; 00483 00484 class LIBQUTIM_EXPORT MultiLineStringDataItem : public DataItem 00485 { 00486 public: 00487 MultiLineStringDataItem(const QString &name, const LocalizedString &title, 00488 const QString &data = QString()); 00489 MultiLineStringDataItem(const QString &name, const LocalizedString &title, 00490 const LocalizedString &data); 00491 }; 00492 00493 class LIBQUTIM_EXPORT StringDataItem : public DataItem 00494 { 00495 public: 00496 StringDataItem(const QString &name, const LocalizedString &title, 00497 const QString &data = QString(), 00498 QValidator *validator = 0, bool password = false); 00499 StringDataItem(const QString &name, const LocalizedString &title, 00500 const QString &data, QRegExp validator, 00501 bool password = false); 00502 StringDataItem(const QString &name, const LocalizedString &title, 00503 const LocalizedString &data, QValidator *validator = 0, 00504 bool password = false); 00505 StringDataItem(const QString &name, const LocalizedString &title, 00506 const LocalizedString &data, QRegExp validator, 00507 bool password = false); 00508 }; 00509 00510 class LIBQUTIM_EXPORT AbstractDataForm : public QWidget 00511 { 00512 Q_OBJECT 00513 public: 00514 enum ButtonRole { 00515 InvalidRole = -1, 00516 AcceptRole, 00517 RejectRole, 00518 DestructiveRole, 00519 ActionRole, 00520 HelpRole, 00521 YesRole, 00522 NoRole, 00523 ResetRole, 00524 ApplyRole, 00525 00526 NRoles 00527 }; 00528 Q_DECLARE_FLAGS(ButtonRoles, ButtonRole) 00529 enum StandardButton { 00530 NoButton = 0x00000000, 00531 Ok = 0x00000400, 00532 Save = 0x00000800, 00533 SaveAll = 0x00001000, 00534 Open = 0x00002000, 00535 Yes = 0x00004000, 00536 YesToAll = 0x00008000, 00537 No = 0x00010000, 00538 NoToAll = 0x00020000, 00539 Abort = 0x00040000, 00540 Retry = 0x00080000, 00541 Ignore = 0x00100000, 00542 Close = 0x00200000, 00543 Cancel = 0x00400000, 00544 Discard = 0x00800000, 00545 Help = 0x01000000, 00546 Apply = 0x02000000, 00547 Reset = 0x04000000, 00548 RestoreDefaults = 0x08000000 00549 }; 00550 Q_DECLARE_FLAGS(StandardButtons, StandardButton) 00551 struct Button 00552 { 00553 LocalizedString name; 00554 ButtonRole role; 00555 }; 00556 typedef QList<Button> Buttons; 00557 public: 00558 virtual DataItem item() const = 0; 00565 virtual bool isChanged() const = 0; 00573 virtual bool isComplete() const = 0; 00577 virtual void clearState() = 0; 00584 virtual void setData(const QString &fieldName, const QVariant &data) = 0; 00585 static AbstractDataForm *get(const DataItem &item, 00586 StandardButtons standartButtons = NoButton, 00587 const Buttons &buttons = Buttons()); 00588 public slots: 00589 void accept(); 00590 void reject(); 00591 signals: 00592 void accepted(); 00593 void rejected(); 00594 void helpRequested(); 00595 void clicked(int buttonIndex); 00596 void clicked(qutim_sdk_0_3::AbstractDataForm::StandardButton standartButton); 00601 void changed(); 00608 void completeChanged(bool complete); 00609 protected: 00610 virtual void virtual_hook(int id, void *data); 00611 }; 00612 00613 class LIBQUTIM_EXPORT DataFormsBackend : public QObject 00614 { 00615 Q_OBJECT 00616 public: 00617 virtual AbstractDataForm *get(const DataItem &item, 00618 AbstractDataForm::StandardButtons standartButtons = AbstractDataForm::NoButton, 00619 const AbstractDataForm::Buttons &buttons = AbstractDataForm::Buttons()) = 0; 00620 static DataFormsBackend *instance(); 00621 }; 00622 00623 template<typename T> 00624 T DataItem::data(const T &def) const 00625 { 00626 QVariant d = data(); 00627 return d.canConvert<T>() ? d.value<T>() : def; 00628 } 00629 00630 #ifndef Q_QDOC 00631 Q_DECLARE_OPERATORS_FOR_FLAGS(AbstractDataForm::ButtonRoles) 00632 Q_DECLARE_OPERATORS_FOR_FLAGS(AbstractDataForm::StandardButtons) 00633 #endif 00634 } 00635 00636 #ifndef Q_QDOC 00637 Q_DECLARE_METATYPE(qutim_sdk_0_3::DataItem) 00638 Q_DECLARE_METATYPE(QList<qutim_sdk_0_3::DataItem>) 00639 #endif 00640 00641 #endif // DATAFORMS_H 00642