00001
00002 #include "screenwizard.h"
00003
00004
00005 #include <QStringList>
00006 #include <QCoreApplication>
00007 #include <QString>
00008
00009
00010 #include "mythcorecontext.h"
00011 #include "mythmainwindow.h"
00012 #include "mythuihelper.h"
00013 #include "mythlogging.h"
00014
00015 using namespace std;
00016
00017
00018 ScreenWizard::ScreenWizard(MythScreenStack *parent, const char *name) :
00019 MythScreenType(parent, name),
00020 m_x_offset(0), m_y_offset(0),
00021 m_whicharrow(true),
00022 m_coarsefine(false),
00023 m_changed(false),
00024 m_fine(1),
00025 m_coarse(10),
00026 m_change(m_fine),
00027 m_topleftarrow_x(0), m_topleftarrow_y(0),
00028 m_bottomrightarrow_x(0), m_bottomrightarrow_y(0),
00029 m_arrowsize_x(30),
00030 m_arrowsize_y(30),
00031 m_screenwidth(0), m_screenheight(0),
00032 m_xsize(GetMythMainWindow()->GetUIScreenRect().width()),
00033 m_ysize(GetMythMainWindow()->GetUIScreenRect().height()),
00034 m_menuPopup(NULL)
00035 {
00036
00037
00038 getSettings();
00039 getScreenInfo();
00040 }
00041
00042 ScreenWizard::~ScreenWizard() {}
00043
00044
00045 bool ScreenWizard::Create()
00046 {
00047 bool foundtheme = false;
00048
00049
00050 foundtheme = LoadWindowFromXML("appear-ui.xml", "appearance", this);
00051
00052 if (!foundtheme)
00053 return false;
00054
00055 m_topleftarrow = dynamic_cast<MythUIImage *> (GetChild("topleft"));
00056 m_bottomrightarrow = dynamic_cast<MythUIImage *> (GetChild("bottomright"));
00057
00058 m_size = dynamic_cast<MythUIText *> (GetChild("size"));
00059 m_offsets = dynamic_cast<MythUIText *> (GetChild("offsets"));
00060 m_changeamount = dynamic_cast<MythUIText *> (GetChild("changeamount"));
00061 if (!m_topleftarrow || !m_bottomrightarrow || !m_size || !m_offsets ||
00062 !m_changeamount)
00063 {
00064 LOG(VB_GENERAL, LOG_ERR, "ScreenWizard, Error: "
00065 "Could not instantiate, please check appear-ui.xml for errors");
00066 return false;
00067 }
00068
00069 m_arrowsize_x = m_topleftarrow->GetArea().width();
00070 m_arrowsize_y = m_topleftarrow->GetArea().height();
00071
00072
00073 m_topleftarrow_x = m_xoffset;
00074 m_topleftarrow_y = m_yoffset;
00075 m_bottomrightarrow_x = m_screenwidth - m_xoffset - m_arrowsize_x;
00076 m_bottomrightarrow_y = m_screenheight - m_yoffset - m_arrowsize_y;
00077
00078 setContext(1);
00079 updateScreen();
00080
00081 return true;
00082 }
00083
00084 void ScreenWizard::setContext(int context)
00085 {
00086
00087 if (context == 1)
00088 {
00089 m_topleftarrow->SetVisible(true);
00090 m_bottomrightarrow->SetVisible(false);
00091 }
00092 else if (context == 2)
00093 {
00094 m_bottomrightarrow->SetVisible(true);
00095 m_topleftarrow->SetVisible(false);
00096 }
00097
00098 }
00099
00100 void ScreenWizard::getSettings()
00101 {
00102 float m_wmult = 0, m_hmult = 0;
00103 GetMythUI()->GetScreenSettings(m_screenwidth, m_wmult, m_screenheight, m_hmult);
00104 }
00105
00106 void ScreenWizard::getScreenInfo()
00107 {
00108 m_xoffset_old = gCoreContext->GetNumSetting("GuiOffsetX", 0);
00109 m_yoffset_old = gCoreContext->GetNumSetting("GuiOffsetY", 0);
00110 m_xoffset = m_xoffset_old;
00111 m_yoffset = m_yoffset_old;
00112 }
00113
00114
00115 bool ScreenWizard::keyPressEvent(QKeyEvent *event)
00116 {
00117 if (GetFocusWidget() && GetFocusWidget()->keyPressEvent(event))
00118 return true;
00119
00120 QStringList actions;
00121 bool handled = false;
00122
00123 handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
00124
00125 for (int i = 0; i < actions.size() && !handled; i++)
00126 {
00127 QString action = actions[i];
00128 handled = true;
00129
00130 if (action == "SELECT")
00131 swapArrows();
00132 else if (action == "UP")
00133 moveUp();
00134 else if (action == "DOWN")
00135 moveDown();
00136 else if (action == "LEFT")
00137 moveLeft();
00138 else if (action == "RIGHT")
00139 moveRight();
00140 else if (action == "MENU")
00141 doMenu();
00142 else
00143 handled = false;
00144 }
00145
00146 if (!handled && MythScreenType::keyPressEvent(event))
00147 handled = true;
00148
00149 return handled;
00150 }
00151
00152 void ScreenWizard::swapArrows()
00153 {
00154 if (m_whicharrow)
00155 {
00156 setContext(2);
00157 m_whicharrow = false;
00158 }
00159
00160 else
00161 {
00162 setContext(1);
00163 m_whicharrow = true;
00164 }
00165
00166
00167 updateScreen();
00168 }
00169
00170 void ScreenWizard::moveUp()
00171 {
00172 if (m_whicharrow)
00173 {
00174 m_topleftarrow_y -= m_change;
00175 if (m_topleftarrow_y < 0)
00176 m_topleftarrow_y = 0;
00177 }
00178 else
00179 {
00180 m_bottomrightarrow_y -= m_change;
00181 if (m_bottomrightarrow_y < int(m_screenheight * 0.75))
00182 m_bottomrightarrow_y = int(m_screenheight * 0.75);
00183 }
00184
00185 updateScreen();
00186 anythingChanged();
00187 }
00188
00189 void ScreenWizard::moveLeft()
00190 {
00191 if (m_whicharrow)
00192 {
00193 m_topleftarrow_x -= m_change;
00194 if (m_topleftarrow_x < 0)
00195 m_topleftarrow_x = 0;
00196 }
00197 else
00198 {
00199 m_bottomrightarrow_x -= m_change;
00200 if (m_bottomrightarrow_x < int(m_screenwidth * 0.75))
00201 m_bottomrightarrow_x = int(m_screenwidth * 0.75);
00202 }
00203
00204 updateScreen();
00205 anythingChanged();
00206 }
00207
00208 void ScreenWizard::moveDown()
00209 {
00210 if (m_whicharrow)
00211 {
00212 {
00213 m_topleftarrow_y += m_change;
00214 if (m_topleftarrow_y > int(m_screenheight * 0.25))
00215 m_topleftarrow_y = int(m_screenheight * 0.25);
00216 }
00217 }
00218 else
00219 {
00220 m_bottomrightarrow_y += m_change;
00221 if (m_bottomrightarrow_y > m_screenheight - m_arrowsize_y)
00222 m_bottomrightarrow_y = m_screenheight - m_arrowsize_y;
00223 }
00224
00225 updateScreen();
00226 anythingChanged();
00227 }
00228
00229 void ScreenWizard::moveRight()
00230 {
00231 if (m_whicharrow)
00232 {
00233 m_topleftarrow_x += m_change;
00234 if (m_topleftarrow_x > int (m_screenwidth * 0.25))
00235 m_topleftarrow_x = int (m_screenwidth * 0.25);
00236 }
00237 else
00238 {
00239 m_bottomrightarrow_x += m_change;
00240 if (m_bottomrightarrow_x > m_screenwidth - m_arrowsize_x)
00241 m_bottomrightarrow_x = m_screenwidth - m_arrowsize_x;
00242 }
00243
00244 updateScreen();
00245 anythingChanged();
00246 }
00247
00248 void ScreenWizard::updateScreen()
00249 {
00250 m_xsize = (m_bottomrightarrow_x - m_topleftarrow_x + m_arrowsize_x);
00251 m_ysize = (m_bottomrightarrow_y - m_topleftarrow_y + m_arrowsize_y);
00252 m_xoffset = m_topleftarrow_x;
00253 m_yoffset = m_topleftarrow_y;
00254 m_topleftarrow->SetPosition(QPoint(m_topleftarrow_x, m_topleftarrow_y));
00255 m_bottomrightarrow->SetPosition(QPoint(m_bottomrightarrow_x, m_bottomrightarrow_y));
00256 m_size->SetText(tr("Size: %1 x %2").arg(m_xsize).arg(m_ysize));
00257 m_offsets->SetText(tr("Offset: %1 x %2").arg(m_xoffset).arg(m_yoffset));
00258 m_changeamount->SetText(tr("Change amount: %n pixel(s)", "", m_change));
00259
00260 }
00261
00262 void ScreenWizard::doMenu()
00263 {
00264 if (m_menuPopup)
00265 return;
00266
00267 QString label = tr("Options");
00268
00269 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00270
00271 m_menuPopup = new MythDialogBox(label, popupStack, "menuPopup");
00272
00273 if (m_menuPopup->Create())
00274 {
00275 if (m_changed)
00276 {
00277 m_menuPopup->SetReturnEvent(this, "save");
00278 m_menuPopup->AddButton(tr("Save and Quit"));
00279 }
00280 else
00281 m_menuPopup->SetReturnEvent(this, "nosave");
00282
00283 m_menuPopup->AddButton(tr("Reset Changes and Quit"));
00284 m_menuPopup->AddButton(tr("Coarse/Fine adjustment"));
00285 m_menuPopup->AddButton(tr("Close Menu"));
00286
00287 popupStack->AddScreen(m_menuPopup);
00288 }
00289 else
00290 {
00291 delete m_menuPopup;
00292 }
00293 }
00294
00295 void ScreenWizard::slotSaveSettings()
00296 {
00297 LOG(VB_GENERAL, LOG_ERR, "Updating screen size settings");
00298 updateSettings();
00299 }
00300
00301 void ScreenWizard::slotChangeCoarseFine()
00302 {
00303 if (m_coarsefine)
00304 {
00305 m_coarsefine = false;
00306 m_change = m_fine;
00307 }
00308 else
00309 {
00310 m_coarsefine = true;
00311 m_change = m_coarse;
00312 }
00313
00314 updateScreen();
00315 }
00316
00317 void ScreenWizard::updateSettings()
00318 {
00319 gCoreContext->SaveSetting("GuiOffsetX", m_xoffset);
00320 gCoreContext->SaveSetting("GuiOffsetY", m_yoffset);
00321 gCoreContext->SaveSetting("GuiWidth", m_xsize);
00322 gCoreContext->SaveSetting("GuiHeight", m_ysize);
00323 LOG(VB_GENERAL, LOG_ERR, "Updated screen size settings");
00324 GetMythMainWindow()->JumpTo("Reload Theme");
00325 }
00326
00327 void ScreenWizard::slotResetSettings()
00328 {
00329 gCoreContext->SaveSetting("GuiOffsetX", 0);
00330 gCoreContext->SaveSetting("GuiOffsetY", 0);
00331 gCoreContext->SaveSetting("GuiWidth", 0);
00332 gCoreContext->SaveSetting("GuiHeight", 0);
00333 m_changed = false;
00334 GetMythMainWindow()->JumpTo("Reload Theme");
00335 }
00336
00337 void ScreenWizard::anythingChanged()
00338 {
00339 if (m_xsize != m_screenwidth)
00340 m_changed = true;
00341 else if (m_xoffset != m_xoffset_old)
00342 m_changed = true;
00343 else if (m_ysize != m_screenheight)
00344 m_changed = true;
00345 else if (m_yoffset != m_yoffset_old)
00346 m_changed = true;
00347 else m_changed = false;
00348 }
00349
00350 void ScreenWizard::customEvent(QEvent *event)
00351 {
00352
00353 if (event->type() == DialogCompletionEvent::kEventType)
00354 {
00355 DialogCompletionEvent *dce = (DialogCompletionEvent*)(event);
00356
00357 QString resultid = dce->GetId();
00358 int buttonnum = dce->GetResult();
00359
00360 if (resultid == "save")
00361 {
00362 if (buttonnum == 0)
00363 slotSaveSettings();
00364 else if (buttonnum == 1)
00365 slotResetSettings();
00366 else if (buttonnum == 2)
00367 slotChangeCoarseFine();
00368 }
00369 else if (resultid == "nosave")
00370 {
00371 if (buttonnum == 0)
00372 slotResetSettings();
00373 if (buttonnum == 1)
00374 slotChangeCoarseFine();
00375 }
00376
00377 m_menuPopup = NULL;
00378 }
00379
00380 }