#ifndef OBLIKUESTRATEGIES_H #define OBLIKUESTRATEGIES_H #include <Plasma/Applet> #include <Plasma/Label> #include <ui_configwindow.h> class QGraphicsLinearLayout; class oblikuestrategies : public Plasma::Applet { Q_OBJECT public: oblikuestrategies(QObject *parent, const QVariantList &args); ~oblikuestrategies(); int setMessagesText(); void init(); public slots: int autoUpdateEvent(); int sendNotification(QString eventId, int num); int updateEvent(); void mousePressEvent(QGraphicsSceneMouseEvent *event); // for configuration interface int setAutoUpdate(); void configAccepted(); void configChanged(); protected: void createConfigurationInterface(KConfigDialog *parent); private: // ui Plasma::Label *main_label; Plasma::Label *info_label; QTimer *timer; // variables bool autoUpdate_bool, notify_bool; int autoUpdate_int, edition, fontSize, fontWeight; QString fontFamily, fontColor, fontStyle; QStringList formatLine, copyright; QList<QStringList> mess; // configuration interface Ui::ConfigWindow uiConfig; }; K_EXPORT_PLASMA_APPLET(oblikue-strategies, oblikuestrategies) #endif /* OBLIKUESTRATEGIES_H */
#include "oblikue-strategies.h" #include <QGraphicsLinearLayout> #include <plasma/theme.h> oblikuestrategies::oblikuestrategies(QObject *parent, const QVariantList &args) : Plasma::Applet(parent, args) { setBackgroundHints(DefaultBackground); setHasConfigurationInterface(true); } oblikuestrategies::~oblikuestrategies() { delete info_label; delete main_label; delete timer; }
void oblikuestrategies::init() { if (setMessagesText() != 0) return; // generate ui // layout QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this); layout->setOrientation(Qt::Vertical); // label layout->addStretch(1); main_label = new Plasma::Label(this); main_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); main_label->setToolTip(qApp->translate("tooltip", "Click here to update message")); layout->addItem(main_label); layout->addStretch(1); // copyright label info_label = new Plasma::Label(this); layout->addItem(info_label); }
void oblikuestrategies::configChanged() { KConfigGroup cg = config(); edition = cg.readEntry("edition", 1); fontFamily = cg.readEntry("font_family", "Terminus"); ... }
void oblikuestrategies::configAccepted() { KConfigGroup cg = config(); cg.writeEntry("edition", uiConfig.comboBox_edition->currentIndex()+1); cg.writeEntry("font_family", uiConfig.fontComboBox_font->currentFont().family()); ... }
void oblikuestrategies::createConfigurationInterface(KConfigDialog *parent) { QWidget *configwin = new QWidget; uiConfig.setupUi(configwin); uiConfig.comboBox_edition->setCurrentIndex(edition-1); ... parent->addPage(configwin, i18n("Oblikue Strategies"), Applet::icon()); connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted())); }
void oblikuestrategies::mousePressEvent(QGraphicsSceneMouseEvent *event) { // mouse click event if (event->buttons() == Qt::LeftButton) updateEvent(); }
... // timer timer = new QTimer(this); timer->setSingleShot(false); ...
if (autoUpdate_bool == true) { disconnect(timer, SIGNAL(timeout()), this, SLOT(autoUpdateEvent())); timer->stop(); } // ... if (autoUpdate_bool == true) { connect(timer, SIGNAL(timeout()), this, SLOT(autoUpdateEvent())); timer->start(autoUpdate_int * MSEC_IN_MIN); }
int oblikuestrategies::autoUpdateEvent() { // auto update text int num = updateEvent(); if (notify_bool == true) if (sendNotification(QString("newMessage"), num) != 0) return 1; return 0; }
[Global] IconName=oblikue-strategies Name=Oblikue Strategies Comment=Oblikue Strategies [Event/newMessage] Name=New message Comment=There is auto updated message in widget Action=Popup
int oblikuestrategies::sendNotification(QString eventId, int num) { // send notification KNotification *notification = new KNotification(eventId); notification->setComponentData(KComponentData("plasma_applet_oblikue-strategies")); notification->setTitle(QString(i18n("Oblikue Strategies"))); notification->setText(mess[edition-1][num]); notification->sendEvent(); delete notification; return 0; }
CMakeLists.txt configwindow.ui oblikue-strategies.cpp oblikue-strategies.h oblikue-strategies.png plasma-applet-oblikue-strategies.desktop plasma_applet_oblikue-strategies.notifyrc
project (plasma_applet_oblikue-strategies) find_package (KDE4 REQUIRED) include (KDE4Defaults) add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS}) include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES}) set (PLUGIN_NAME ${PROJECT_NAME}) file (GLOB PROJECT_DESKTOP *.desktop) file (GLOB PROJECT_ICON *.png) file (GLOB PROJECT_NOTIFY *.notifyrc) file (GLOB PROJECT_SOURCE *.cpp) file (GLOB PROJECT_UI *.ui) kde4_add_ui_files (PROJECT_SOURCE ${PROJECT_UI}) kde4_add_plugin (${PLUGIN_NAME} ${PROJECT_SOURCE}) target_link_libraries (${PLUGIN_NAME} ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS}) # install install (TARGETS ${PLUGIN_NAME} DESTINATION ${PLUGIN_INSTALL_DIR}) install (FILES ${PROJECT_DESKTOP} DESTINATION ${SERVICES_INSTALL_DIR}) install (FILES ${PROJECT_ICON} DESTINATION ${ICON_INSTALL_DIR}) install (FILES ${PROJECT_NOTIFY} DESTINATION ${DATA_INSTALL_DIR}/${PLUGIN_NAME})
Source: https://habr.com/ru/post/192698/
All Articles