Difference between revisions of "Building Plugins:MythNotes01"
From MythTV Official Wiki
m (→C++ code) |
m (→C++ code) |
||
Line 138: | Line 138: | ||
{{Code box|/mythnotes/notesUI.cpp| | {{Code box|/mythnotes/notesUI.cpp| | ||
+ | <pre> | ||
/* | /* | ||
* Source file for UIs | * Source file for UIs |
Latest revision as of 16:47, 10 May 2020
Demonstrates some of available UIs.
- Use XML to define a page with multiple UI widgets
- Create basic signals and slots to handle the UI communication
Note: Read the MythNotes introduction Building_Plugins:MythNotes first.
Note: This tutorial follows-up the Building_Plugins:MythNotes00 tutorial.
C++ code
/* * Main of MythTV's demonstration plugin MythNotes * * Copyright (C) 2010 Lukas Doktor <ldoktor@redhat.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * */ #include <iostream> using namespace std; #include <QApplication> #include "notesUI.h" #include <mythversion.h> #include <mythpluginapi.h> #include <mythscreentype.h> #include <mythuihelper.h> #include <mythmainwindow.h> /* Main program run wrappers */ void runNotes(void); int RunNotes(void); /* UNCOMPLETED plugin initialization, see following examples */ int mythplugin_init(const char *libversion) { return 0; } /* plugin execution wrapper */ void runNotes(void) { RunNotes(); } /* plugin execution wrapper */ int RunNotes(void) { MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack(); NotesUI *notes = new NotesUI(mainStack, "Notes"); if (notes->Create()) { mainStack->AddScreen(notes); return 0; } else { delete notes; return -1; } } /* plugin execution */ int mythplugin_run(void) { return RunNotes(); }
No change in this file.
/* * Header file for UI * * Copyright (C) 2010 Lukas Doktor <ldoktor@redhat.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * */ #ifndef NOTESUI_H_ #define NOTESUI_H_ #include <QString> #include <QKeyEvent> #include <mythscreentype.h> #include <mythmainwindow.h> #include <mythdialogbox.h> #include <mythuibutton.h> class MythUIButton; class MythUIText; class MythUITextEdit; class QKeyEvent; class NotesUI : public MythScreenType { Q_OBJECT public: NotesUI(MythScreenStack *parentStack, QString name = "MythNotes"); bool Create() override; // MythScreenType bool keyPressEvent(QKeyEvent *event) override; // MythScreenType private: MythUIText *m_title; MythUIText *m_notes; MythUITextEdit *m_newnote; MythUIButton *m_addBtn; MythUIButton *m_closeBtn; private slots: void addCallback(); void closeCallback(); }; #endif /* NOTESUI_H_ */
- keyPressEvent() function is used to custom handling of QKeyEvents
- couple of private variables are used to handle the XML objects. Notice the different types - MythUIText, MythUITextEdit and MythUIButton. (See the "mythtv-svn-trunk/mythtv/libs/libmythui/*" files for details)
- slots are used in QT to connect events. In this example they are connected with various buttons.
/* * Source file for UIs * * Copyright (C) 2010 Lukas Doktor <ldoktor@redhat.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * */ #include <mythuitext.h> #include "notesUI.h" NotesUI::NotesUI(MythScreenStack *parent, QString name) : MythScreenType(parent, name), m_title(NULL), m_notes(NULL), m_newnote(NULL), m_addBtn(NULL), m_closeBtn(NULL) { } bool NotesUI::Create() { if (!LoadWindowFromXML("notes-ui.xml", "notesui", this)) return false; bool err = false; UIUtilE::Assign(this, m_title, "title", &err); UIUtilE::Assign(this, m_notes, "notes", &err); UIUtilE::Assign(this, m_newnote, "newnote", &err); UIUtilE::Assign(this, m_addBtn, "addbtn", &err); UIUtilE::Assign(this, m_closeBtn, "closebtn", &err); if (err) return false; connect(m_addBtn, SIGNAL(Clicked()), this, SLOT(addCallback())); connect(m_closeBtn, SIGNAL(Clicked()), this, SLOT(closeCallback())); BuildFocusList(); SetFocusWidget(m_newnote); return true; } bool NotesUI::keyPressEvent(QKeyEvent *event) { if (GetFocusWidget()->keyPressEvent(event)) return true; bool handled = false; QStringList actions; handled = GetMythMainWindow()->TranslateKeyPress("Notes", event, actions); for (int i = 0; i < actions.size() && !handled; i++) { QString action = actions[i]; handled = true; if (action == "ESCAPE") { closeCallback(); } else { handled = false; } } if (!handled && MythScreenType::keyPressEvent(event)) handled = true; return handled; } void NotesUI::addCallback() { m_notes->SetText(m_notes->GetText() + "\n\t" + m_newnote->GetText()); } void NotesUI::closeCallback() { Close(); }
- NotesUI::NotesUI - constructor
- Don't forget to always initialize all variables/objects
- NotesUI::Create
- Loads the xml and assigns all the variables with according named objects
- connects(object_A, signal_from_object_A, object_B, signal_from_object_B) - connect the Clicked() signal from button with this window's slot ...Callback
- Work with objects/widgets focuses. You can even set some of the widget unfocusable.
- NotesUI::keyPressEvent
- You can copy this function from another plug-in and customize. It's used to create named keys which can user map to - for him - suitable keys.
- NotesUI::addCallback
- Slot which adds the text from one field to the textarea
- NotesUI::closeCallback
- Slot which closes this window
UI files
We need to define additional buttons and text areas/boxes:
<?xml version="1.0" encoding="utf-8"?> <mythuitheme> <window name="notesui"> <textarea name="title"> <area>10,10,780,60</area> <font>baselarge</font> <align>allcenter</align> <multiline>yes</multiline> <value>01 Basic UI</value> </textarea> <textarea name="notes"> <area>10,80,395,510</area> <font>basemedium</font> <align>left,top</align> <multiline>yes</multiline> </textarea> <textedit name="newnote" from="basetextedit"> <area>405,90,395,50</area> </textedit> <button name="addbtn" from="basewidebutton"> <position>405,150</position> <value>Add note</value> </button> <button name="closebtn" from="basewidebutton"> <position>405,200</position> <value>Close</value> </button> </window> </mythuitheme>
- Positions of the objects are relative to the basic resolution (800x600 - 4x3 or 1280x720 - 16:9).
- "from" modifier used in "button" tag is used to define the type of the object. You can specify your own or (more commonly) use the default ones from "/usr/share/mythtv/themes/default/base.xml".
Remaining files
No changes here
Compile & Run
See the first part Building_Plugins:MythNotes00.
Recapitulation
- copy the plug-in from the first part (Building_Plugins:MythNotes00)
- Modify the C++ code
- Modify the XML UI layout
Next tutorial
Note: You have successfully finished the Basic UI tutorial. Let's move on to the next level - "Complete plugin" (Building_Plugins:MythNotes02)