MythTV  0.26-pre
themeselector.cpp
Go to the documentation of this file.
00001 // c
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <cstdlib>
00005 
00006 // qt
00007 #include <QDir>
00008 #include <QFile>
00009 #include <QFileInfo>
00010 #include <QKeyEvent>
00011 #include <QTextStream>
00012 #include <QCoreApplication>
00013 
00014 // myth
00015 #include <mythcontext.h>
00016 #include <mythdirs.h>
00017 #include <mythuihelper.h>
00018 #include <mythuitext.h>
00019 #include <mythuibutton.h>
00020 #include <mythuiimage.h>
00021 #include <mythuibuttonlist.h>
00022 #include <mythmainwindow.h>
00023 
00024 // mytharchive
00025 #include "mythburn.h"
00026 #include "themeselector.h"
00027 
00028 DVDThemeSelector::DVDThemeSelector(
00029     MythScreenStack *parent, MythScreenType *destinationScreen,
00030     ArchiveDestination archiveDestination, QString name) :
00031     MythScreenType(parent, name, true),
00032     m_destinationScreen(destinationScreen),
00033     m_archiveDestination(archiveDestination),
00034     themeDir(GetShareDir() + "mytharchive/themes/"),
00035     theme_selector(NULL),
00036     theme_image(NULL),
00037     theme_no(0),
00038     intro_image(NULL),
00039     mainmenu_image(NULL),
00040     chapter_image(NULL),
00041     details_image(NULL),
00042     themedesc_text(NULL),
00043     m_nextButton(NULL),
00044     m_prevButton(NULL),
00045     m_cancelButton(NULL)
00046 {
00047 }
00048 
00049 DVDThemeSelector::~DVDThemeSelector(void)
00050 {
00051     saveConfiguration();
00052 }
00053 
00054 bool DVDThemeSelector::Create(void)
00055 {
00056     bool foundtheme = false;
00057 
00058     // Load the theme for this screen
00059     foundtheme = LoadWindowFromXML("mythburn-ui.xml", "themeselector", this);
00060 
00061     if (!foundtheme)
00062         return false;
00063 
00064     bool err = false;
00065     UIUtilE::Assign(this, m_nextButton, "next_button", &err);
00066     UIUtilE::Assign(this, m_prevButton, "prev_button", &err);
00067     UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
00068 
00069         // theme preview images
00070     UIUtilE::Assign(this, intro_image, "intro_image", &err);
00071     UIUtilE::Assign(this, mainmenu_image, "mainmenu_image", &err);
00072     UIUtilE::Assign(this, chapter_image, "chapter_image", &err);
00073     UIUtilE::Assign(this, details_image, "details_image", &err);
00074 
00075         // menu theme
00076     UIUtilE::Assign(this, themedesc_text, "themedescription", &err);
00077     UIUtilE::Assign(this, theme_image, "theme_image", &err);
00078     UIUtilE::Assign(this, theme_selector, "theme_selector", &err);
00079 
00080     if (err)
00081     {
00082         LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'themeselector'");
00083         return false;
00084     }
00085 
00086     connect(m_nextButton, SIGNAL(Clicked()), this, SLOT(handleNextPage()));
00087     connect(m_prevButton, SIGNAL(Clicked()), this, SLOT(handlePrevPage()));
00088     connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(handleCancel()));
00089 
00090     getThemeList();
00091 
00092     connect(theme_selector, SIGNAL(itemSelected(MythUIButtonListItem*)),
00093             this, SLOT(themeChanged(MythUIButtonListItem*)));
00094 
00095     BuildFocusList();
00096 
00097     SetFocusWidget(m_nextButton);
00098 
00099     loadConfiguration();
00100 
00101     return true;
00102 }
00103 
00104 bool DVDThemeSelector::keyPressEvent(QKeyEvent *event)
00105 {
00106     if (GetFocusWidget()->keyPressEvent(event))
00107         return true;
00108 
00109     if (MythScreenType::keyPressEvent(event))
00110         return true;
00111 
00112     return false;
00113 }
00114 
00115 void DVDThemeSelector::handleNextPage()
00116 {
00117     saveConfiguration();
00118 
00119     // show next page
00120     MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00121 
00122     MythBurn *burn = new MythBurn(mainStack, m_destinationScreen, this,
00123                                   m_archiveDestination, "MythBurn");
00124 
00125     if (burn->Create())
00126         mainStack->AddScreen(burn);
00127 }
00128 
00129 void DVDThemeSelector::handlePrevPage()
00130 {
00131     Close();
00132 }
00133 
00134 void DVDThemeSelector::handleCancel()
00135 {
00136     m_destinationScreen->Close();
00137     Close();
00138 }
00139 
00140 void DVDThemeSelector::getThemeList(void)
00141 {
00142     theme_list.clear();
00143     QDir d;
00144 
00145     d.setPath(themeDir);
00146     if (d.exists())
00147     {
00148         QStringList filters;
00149         filters << "*";
00150         QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);
00151 
00152         int count = 0;
00153         for (int x = 0; x < list.size(); x++)
00154         {
00155             QFileInfo fi = list.at(x);
00156             if (QFile::exists(themeDir + fi.fileName() + "/preview.png"))
00157             {
00158                 theme_list.append(fi.fileName());
00159                 QString filename = fi.fileName().replace(QString("_"), QString(" "));
00160                 new MythUIButtonListItem(theme_selector, filename);
00161                 ++count;
00162             }
00163         }
00164     }
00165     else
00166         LOG(VB_GENERAL, LOG_ERR,
00167             "MythArchive:  Theme directory does not exist!");
00168 }
00169 
00170 void DVDThemeSelector::themeChanged(MythUIButtonListItem *item)
00171 {
00172     if (!item)
00173         return;
00174 
00175     int itemNo = theme_selector->GetCurrentPos();
00176 
00177     if (itemNo < 0 || itemNo > theme_list.size() - 1)
00178         itemNo = 0;
00179 
00180     theme_no = itemNo;
00181 
00182     if (QFile::exists(themeDir + theme_list[itemNo] + "/preview.png"))
00183         theme_image->SetFilename(themeDir + theme_list[itemNo] + "/preview.png");
00184     else
00185         theme_image->SetFilename("blank.png");
00186     theme_image->Load();
00187 
00188     if (QFile::exists(themeDir + theme_list[itemNo] + "/intro_preview.png"))
00189         intro_image->SetFilename(themeDir + theme_list[itemNo] + "/intro_preview.png");
00190     else
00191         intro_image->SetFilename("blank.png");
00192     intro_image->Load();
00193 
00194     if (QFile::exists(themeDir + theme_list[itemNo] + "/mainmenu_preview.png"))
00195         mainmenu_image->SetFilename(themeDir + theme_list[itemNo] + "/mainmenu_preview.png");
00196     else
00197         mainmenu_image->SetFilename("blank.png");
00198     mainmenu_image->Load();
00199 
00200     if (QFile::exists(themeDir + theme_list[itemNo] + "/chaptermenu_preview.png"))
00201         chapter_image->SetFilename(themeDir + theme_list[itemNo] + "/chaptermenu_preview.png");
00202     else
00203         chapter_image->SetFilename("blank.png");
00204     chapter_image->Load();
00205 
00206     if (QFile::exists(themeDir + theme_list[itemNo] + "/details_preview.png"))
00207         details_image->SetFilename(themeDir + theme_list[itemNo] + "/details_preview.png");
00208     else
00209         details_image->SetFilename("blank.png");
00210     details_image->Load();
00211 
00212     if (QFile::exists(themeDir + theme_list[itemNo] + "/description.txt"))
00213     {
00214         QString desc = loadFile(themeDir + theme_list[itemNo] + "/description.txt");
00215         themedesc_text->SetText(QCoreApplication::translate("BurnThemeUI", 
00216                                 desc.toUtf8().constData()));
00217     }
00218     else
00219         themedesc_text->SetText(tr("No theme description file found!"));
00220 }
00221 
00222 QString DVDThemeSelector::loadFile(const QString &filename)
00223 {
00224     QString res = "";
00225 
00226     QFile file(filename);
00227 
00228     if (!file.exists())
00229     {
00230         res = tr("No theme description file found!");
00231     }
00232     else {
00233         if (file.open(QIODevice::ReadOnly))
00234         {
00235             QTextStream stream(&file);
00236 
00237             if (!stream.atEnd())
00238             {
00239                 res = stream.readAll();
00240                 res = res.replace("\n", " ").trimmed();
00241             }
00242             else {
00243                 res = tr("Empty theme description!");
00244             }
00245             file.close();
00246         }
00247         else {
00248            res = tr("Unable to open theme description file!");
00249         }
00250     }
00251 
00252     return res;
00253 }
00254 
00255 void DVDThemeSelector::loadConfiguration(void)
00256 {
00257     QString theme = gCoreContext->GetSetting("MythBurnMenuTheme", "");
00258     theme = theme.replace(QString("_"), QString(" "));
00259     theme_selector->MoveToNamedPosition(theme);
00260 }
00261 
00262 void DVDThemeSelector::saveConfiguration(void)
00263 {
00264     QString theme = theme_selector->GetValue();
00265     theme = theme.replace(QString(" "), QString("_"));
00266     gCoreContext->SaveSetting("MythBurnMenuTheme", theme);
00267 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends