Ticket #5954: dynamic_menus-r19940.patch

File dynamic_menus-r19940.patch, 7.2 KB (added by Nigel, 17 years ago)

Tidyup, test script, against recent SVN

  • libs/libmythui/myththemedmenu.cpp

     
    22#include <QDir>
    33#include <QKeyEvent>
    44#include <QDomDocument>
     5#include <QProcess>
    56
    67#include "myththemedmenu.h"
    78#include "mythmainwindow.h"
     
    8586                               MythScreenStack *parent, const QString &name,
    8687                               bool allowreorder, MythThemedMenuState *state)
    8788    : MythThemedMenuState(parent, name),
    88       m_state(state), m_allocedstate(false), m_foundtheme(false),
     89      m_parent(parent), m_state(state),
     90      m_allocedstate(false), m_foundtheme(false),
    8991      m_exitModifier(-1), m_ignorekeys(false), m_wantpop(false)
    9092{
    91     if (!m_state)
    92     {
    93         m_state = new MythThemedMenuState(parent, "themedmenustate");
    94         m_allocedstate = true;
    95     }
    9693
    9794    Init(menufile);
     95
     96    if (!findAndParseMenu(menufile))
     97        m_foundtheme = false;  // We don't have separate vars for theme & menu
    9898}
    9999
     100
    100101/** \brief Loads the main UI theme, and a menu theme.
    101102 *
    102103 *  See also foundtheme(void), it will return true when called after
     
    106107 */
    107108void MythThemedMenu::Init(const QString &menufile)
    108109{
     110    if (!m_state)
     111    {
     112        m_state = new MythThemedMenuState(m_parent, "themedmenustate");
     113        m_allocedstate = true;
     114    }
     115
    109116    ReloadExitKey();
    110117
    111118    if (!m_state->m_loaded)
     
    125132            SLOT(setButtonActive(MythUIButtonListItem*)));
    126133    connect(m_buttonList, SIGNAL(itemClicked(MythUIButtonListItem*)),
    127134            SLOT(buttonAction(MythUIButtonListItem*)));
    128 
    129     if (!parseMenu(menufile))
    130         m_foundtheme = false;
    131135}
    132136
    133137MythThemedMenu::~MythThemedMenu(void)
     
    136140        delete m_state;
    137141}
    138142
     143/** \brief Creates a dynamic themed menu.
     144 *
     145 *  \param command      external command to execute
     146 *  \param parent       the screen stack that owns this UI type
     147 *  \param state        theme state associated with this menu
     148 */
     149MythThemedMenu::MythThemedMenu(const QString &command,
     150                               MythScreenStack *parent,
     151                               MythThemedMenuState *state)
     152    : MythThemedMenuState(parent, command),
     153      m_parent(parent), m_state(state),
     154      m_allocedstate(false), m_foundtheme(false),
     155      m_exitModifier(-1), m_ignorekeys(false), m_wantpop(false)
     156{
     157    if (command.isEmpty())
     158    {
     159        VERBOSE(VB_IMPORTANT, "Empty MENUEXEC action?");
     160
     161        m_foundtheme = false;
     162        return;
     163    }
     164
     165    Init(command);
     166
     167    QProcess process;
     168    process.start(command);
     169    if (!process.waitForFinished())
     170    {
     171        process.kill();
     172        VERBOSE(VB_IMPORTANT, "Command '" + command + "' returned error");
     173        ShowOkPopup(QObject::tr("Couldn't execute %1").arg(command));
     174
     175        m_foundtheme = false;
     176        return;
     177    }
     178
     179    QString errorMsg;
     180    int errorLine = 0;
     181    int errorColumn = 0;
     182
     183    QDomDocument doc;
     184    if (!doc.setContent(process.readAllStandardOutput(),
     185                        false, &errorMsg, &errorLine, &errorColumn))
     186    {
     187        VERBOSE(VB_IMPORTANT, "Error parsing output of: " + command +
     188                              QString("\nat line: %1  column: %2 msg: %3")
     189                              .arg(errorLine).arg(errorColumn).arg(errorMsg));
     190
     191        ShowOkPopup(QObject::tr("Command %1 returned incomplete menu.")
     192                    .arg(command));
     193
     194        m_foundtheme = false;
     195        return;
     196    }
     197
     198    if (!parseMenu(doc, command))
     199        m_foundtheme = false;  // We don't have separate vars for theme & menu
     200}
    139201/// \brief Returns true iff a theme has been found by a previous call to Init()
    140202bool MythThemedMenu::foundTheme(void)
    141203{
     
    411473 *  non-essential portion of MythTV which the theme does not support.
    412474 *
    413475 */
    414 bool MythThemedMenu::parseMenu(const QString &menuname)
     476bool MythThemedMenu::findAndParseMenu(const QString &menuname)
    415477{
    416478    QString filename = findMenuFile(menuname);
    417479
     
    450512
    451513    VERBOSE(VB_GENERAL, QString("Loading menu theme from %1").arg(filename));
    452514
     515    return parseMenu(doc, menuname);
     516}
     517
     518/** \brief Parse the themebuttons to be added based on the contents
     519 *         of the provided XML document.
     520 */
     521bool MythThemedMenu::parseMenu(const QDomDocument &doc, const QString &menuname)
     522{
    453523    QDomElement docElem = doc.documentElement();
    454524
    455525    m_menumode = docElem.attribute("name", "MAIN");
     
    652722        else
    653723            delete newmenu;
    654724    }
     725    else if (action.left(9) == "MENUEXEC ")
     726    {
     727        QString command = action.mid(9);
     728 
     729        MythScreenStack *stack = GetScreenStack();
     730 
     731        MythThemedMenu *newmenu = new MythThemedMenu(command, stack, m_state);
     732 
     733        if (newmenu->foundTheme())
     734            stack->AddScreen(newmenu);
     735        else
     736            delete newmenu;
     737    }
    655738    else if (action.left(6) == "UPMENU")
    656739    {
    657740        m_wantpop = true;
  • libs/libmythui/myththemedmenu.h

     
    99class MythMainWindow;
    1010class MythThemedMenuState;
    1111
     12class QDomDocument;
    1213class QKeyEvent;
    1314
    1415struct ThemedButton
     
    6667  public:
    6768    MythThemedMenu(const QString &cdir, const QString &menufile,
    6869                    MythScreenStack *parent, const QString &name,
    69                     bool allowreorder = false, MythThemedMenuState *state = NULL);
     70                    bool allowreorder = false,
     71                    MythThemedMenuState *state = NULL);
    7072   ~MythThemedMenu();
    7173
    7274    bool foundTheme(void);
     
    8991  private:
    9092    void Init(const QString &menufile);
    9193
    92     bool parseMenu(const QString &menuname);
     94    bool findAndParseMenu(const QString &menuname);
     95
     96    MythThemedMenu(const QString &command,
     97                   MythScreenStack *parent, MythThemedMenuState *state);
     98
     99    bool parseMenu(const QDomDocument &doc, const QString &menuname);
    93100    void parseThemeButton(QDomElement &element);
    94101
    95102    void addButton(const QString &type, const QString &text,
     
    106113
    107114    void updateLCD(void);
    108115
    109     MythThemedMenu *m_parent;
     116    MythScreenStack *m_parent;
    110117
    111118    MythThemedMenuState *m_state;
    112119    bool m_allocedstate;
  • contrib/menus/random-menu.pl

     
     1#!/usr/bin/perl
     2
     3# random-menu.pl - Hacky test script for MENUEXEC action
     4#
     5# When attached to a button, like this:
     6#   <button>
     7#      <type>RANDOM</type>
     8#      <text>Random Menu</text>
     9#      <action>MENUEXEC /home/nigel/random-menu.pl</action>
     10#   </button>
     11#
     12# it creates a sub-menu randomly chosen from the installed menus.
     13
     14my $menudir = '/usr/local/share/mythtv/*menu.xml';
     15my @menus   = glob($menudir);
     16my $chosen  = $menus[rand($#menus + 1)];  # 10 menus, $#menus=9, rand=0..9.9999
     17
     18print `cat $chosen`;