controlpanelplugins/themeplugin/src/cpthemecontrol.cpp
changeset 40 593f946f4fec
parent 22 a5692c68d772
equal deleted inserted replaced
22:a5692c68d772 40:593f946f4fec
    15  *   
    15  *   
    16  */
    16  */
    17 
    17 
    18 /*!
    18 /*!
    19     \class CpThemeControl
    19     \class CpThemeControl
    20     \brief CpThemeControl creates and controls two views for Theme Changer plugin and handles
    20     \brief CpThemeControl creates and controls views for Theme Changer plugin and handles
    21 	user interaction to preview and change the themes.
    21 	user interaction to preview and change the themes.
    22 
       
    23 	It creates a list view of the themes.  When a list item is selected, it creates a preview
       
    24 	of the theme icon using a CpThemePreview class.  
       
    25 
    22 
    26 	This class also connects to the theme server using the HbThemeChanger and sets the theme
    23 	This class also connects to the theme server using the HbThemeChanger and sets the theme
    27 	based on user interaction with the views.  
    24 	based on user interaction with the views.  
    28 
    25 
    29  */
    26  */
    41 #include <hbinstance.h>
    38 #include <hbinstance.h>
    42 #include "cpthemechanger.h"
    39 #include "cpthemechanger.h"
    43 
    40 
    44 #include "cpthemecontrol.h"
    41 #include "cpthemecontrol.h"
    45 #include "cpthemelistview.h"
    42 #include "cpthemelistview.h"
    46 #include "cpthemepreview.h"
       
    47 #include "cpthemeinfo.h"
    43 #include "cpthemeinfo.h"
    48 #include "cpthemelistmodel.h"
    44 #include "cpthemelistmodel.h"
    49 
    45 
    50 #include <hbdialog.h>
    46 #include <hbdialog.h>
    51 #include <hblabel.h>
    47 #include <hblabel.h>
    52 
    48 
    53 
    49 //time out time before showing a processing dialog.
    54 /*!
    50 static const int KThemeChangeTimeOutMilliSeconds = 2000;  
    55 	Helper function to fetch the main window.
       
    56 */
       
    57 
       
    58 static HbMainWindow *mainWindow() 
       
    59 {
       
    60     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();
       
    61     if (!mainWindows.isEmpty()) {
       
    62         return mainWindows.front();
       
    63     }
       
    64     return 0;
       
    65 }
       
    66 
    51 
    67 /*!
    52 /*!
    68 	constructor.
    53 	constructor.
    69 */
    54 */
    70 CpThemeControl::CpThemeControl(): mThemeListView(0), 
    55 CpThemeControl::CpThemeControl(): mThemeListView(0), 
    71     mThemePreview(0), 
       
    72     mThemeChanger(0),
    56     mThemeChanger(0),
    73     mListModel(0),
    57     mListModel(0),
    74     mThemeChangeFinished(false),
    58     mThemeChangeFinished(false),
    75     mWaitDialog(0)
    59     mWaitDialog(0)
    76 {
    60 {
    86    
    70    
    87 }
    71 }
    88 
    72 
    89 
    73 
    90 /*!
    74 /*!
    91 	destorys the list view, preview and theme changer objects.
    75 	destorys the list view and theme changer objects.
    92 */
    76 */
    93 CpThemeControl::~CpThemeControl()
    77 CpThemeControl::~CpThemeControl()
    94 {
    78 {
    95     delete mThemeListView;
    79     delete mThemeListView;
    96     mThemeListView = 0;
    80     mThemeListView = 0;
    97 
    81 
    98     delete mThemeChanger;
    82     delete mThemeChanger;
    99     mThemeChanger = 0;
    83     mThemeChanger = 0;
   100 
    84 
   101     delete mThemePreview;
       
   102     mThemePreview = 0;
       
   103     
       
   104     delete mWaitDialog;
    85     delete mWaitDialog;
   105     mWaitDialog = 0;
    86     mWaitDialog = 0;
   106 }
    87 }
   107 
    88 
   108 /*!
    89 /*!
   109 	Creates the theme list view.  Gets the themes, creates a model based on
    90 	Creates the theme list view.  Gets the themes, creates a model  and sets the list model.
   110 	theme names, icons, and icon paths and sets the list model.
       
   111 */
    91 */
   112 void CpThemeControl::createThemeList()
    92 void CpThemeControl::createThemeList()
   113 {
    93 {
   114    
    94    
   115     mThemeListView = new CpThemeListView();
    95     mThemeListView = new CpThemeListView();
   118         mListModel = new CpThemeListModel(this);
    98         mListModel = new CpThemeListModel(this);
   119     }
    99     }
   120     
   100     
   121     // Set the model for theme list.
   101     // Set the model for theme list.
   122     mThemeListView->setModel(mListModel);
   102     mThemeListView->setModel(mListModel);
   123     mThemeListView->themeList()->setSelectionMode(HbAbstractItemView::SingleSelection);
       
   124     
   103     
   125     setActiveThemeIndex();
   104     setActiveThemeIndex();
   126     
   105     
   127     
       
   128     //connect to signal for selecting a list item.
   106     //connect to signal for selecting a list item.
   129     connect(mThemeListView,SIGNAL(newThemeSelected(const QModelIndex&)),
   107     connect(mThemeListView,SIGNAL(newThemeSelected(QModelIndex)),
   130             this,SLOT(newThemeSelected(const QModelIndex&)));
   108             this,SLOT(newThemeSelected(QModelIndex)));
       
   109     
       
   110     connect(mThemeListView, SIGNAL(oviClicked()), this, SLOT(getOviTheme()));
   131 
   111 
   132 	//handle signal for list view closing. (e.g Back softkey pressed)
   112 	//handle signal for list view closing. (e.g Back softkey pressed)
   133     connect(mThemeListView,SIGNAL(aboutToClose()),
   113     connect(mThemeListView,SIGNAL(aboutToClose()),
   134             this,SLOT(themeListClosed()));
   114             this,SLOT(themeListClosed()));
   135 }
   115 }
   185     QVariant data;
   165     QVariant data;
   186 
   166 
   187     //reset the current index to active theme, so that the selection remains on current
   167     //reset the current index to active theme, so that the selection remains on current
   188     //theme even though another list item is selected.
   168     //theme even though another list item is selected.
   189     setActiveThemeIndex();
   169     setActiveThemeIndex();
   190     
       
   191     // Figure out whether this is a URI and appropriately delegate
       
   192     data = index.data(CpThemeListModel::ItemTypeRole);
       
   193     if(data.isValid() && data.canConvert<CpThemeInfo::ThemeListItemType>()) {
       
   194 
       
   195         CpThemeInfo::ThemeListItemType type = data.value<CpThemeInfo::ThemeListItemType>();
       
   196 
       
   197         switch (type) {
       
   198             case CpThemeInfo::ThemeListItemType_URL:
       
   199                 //get the URL
       
   200                 data = index.data(CpThemeListModel::ItemDataRole);
       
   201                 if(data.isValid()) {
       
   202                     QString url = data.toString();
       
   203                     // Launch the URL in the browser and 
       
   204                     // continue to Preview if not successful
       
   205                     if (QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode))) {
       
   206                         return;
       
   207                     }
       
   208                 }
       
   209                 break;
       
   210     
       
   211             case CpThemeInfo::ThemeListItemType_APP:
       
   212                 break;
       
   213 
       
   214             default:
       
   215                 // do nothing
       
   216                 qt_noop();
       
   217         }
       
   218     }
       
   219     
   170     
   220     //get the theme name.
   171     //get the theme name.
   221     data = index.data(Qt::DisplayRole);
   172     data = index.data(Qt::DisplayRole);
   222     if(data.isValid()) {
   173     if(data.isValid()) {
   223         themeInfo.setName(data.toString());
   174         themeInfo.setName(data.toString());
   224     }
   175     }
   225     
   176     
   226     //get theme icon.
   177     //get theme path
   227     data = index.data(Qt::DecorationRole);
   178     data = index.data(CpThemeListModel::ItemDataRole);
   228     if(data.isValid()) {
   179     if(data.isValid()) {
   229         themeInfo.setIcon(data.value<HbIcon>());
   180         themeInfo.setItemData(data.toString());
   230     }
   181     }
   231     
   182     
   232     data = index.data(CpThemeListModel::PortraitPreviewRole);
   183     applyTheme(themeInfo);
   233     if(data.isValid()) {
   184 
   234         themeInfo.setPortraitPreviewIcon(data.value<HbIcon>());
   185 }
   235     }
   186 
   236     
   187 void CpThemeControl::getOviTheme()
   237     data = index.data(CpThemeListModel::LandscapePreviewRole);
   188 {
   238     if(data.isValid()) {
   189     QString url = QString("http://lr.ovi.mobi/store/themes");
   239         themeInfo.setLandscapePreviewIcon(data.value<HbIcon>());
   190     // Launch the URL in the browser and 
   240     }
   191     // continue to Preview if not successful
   241     
   192     QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
   242    //Set up the theme preview and set it to
   193                               
   243     //the current view of main window.
       
   244     HbMainWindow*  mWindow = ::mainWindow();
       
   245    
       
   246     if(!mThemePreview){
       
   247         mThemePreview = new CpThemePreview(themeInfo);
       
   248         mWindow->addView(mThemePreview);
       
   249         
       
   250         connect(mThemePreview,SIGNAL(aboutToClose()),
       
   251             this, SLOT(previewClosed()));
       
   252 
       
   253         connect(mThemePreview, SIGNAL(applyTheme(const QString&)),
       
   254                 this, SLOT(themeApplied(const QString&)));
       
   255     } else {
       
   256         mThemePreview->setThemeInfo(themeInfo);
       
   257     }
       
   258     mThemePreview->setTitle(hbTrId("txt_cp_title_control_panel"));
       
   259 
       
   260     mWindow->setCurrentView(mThemePreview);
       
   261 
       
   262 }
   194 }
   263 
   195 
   264 /*!
   196 /*!
   265 	Slot called when a Select key is pressed in theme preview view.
   197 	Slot called when a Select key is pressed in theme preview view.
   266 */
   198 */
   267 void CpThemeControl::themeApplied(const QString& theme)
   199 void CpThemeControl::applyTheme(const CpThemeInfo& theme)
   268 {
   200 {
   269     QThread::currentThread()->setPriority(QThread::HighPriority);  
   201     QThread::currentThread()->setPriority(QThread::HighPriority);  
   270     
   202     
   271     if(mThemeChanger->changeTheme(theme)) {
   203     if(mThemeChanger->changeTheme(theme)) {
   272     
   204     
   273         //Start a timer. If theme change takes more than 1 seconds,
   205         //Start a timer. If theme change takes more than 1 seconds,
   274         //we will show a dialog (mWaitDialog) until theme change
   206         //we will show a dialog (mWaitDialog) until theme change
   275         //is done (themeChangeFinished is called).
   207         //is done (themeChangeFinished is called).
   276         QTimer::singleShot(1000, this, SLOT(themeWaitTimeout()));
   208         QTimer::singleShot(KThemeChangeTimeOutMilliSeconds, this, SLOT(themeWaitTimeout()));
   277         
   209         
   278         mThemeChangeFinished = false;
   210         mThemeChangeFinished = false;
   279     } else {
   211     } else {
   280         //theme change failed, go back to control panel.
   212         //theme change failed, go back to control panel.
   281         previewClosed();
   213         setActiveThemeIndex();
   282         triggerThemeListClose();
       
   283     }
   214     }
   284    
   215    
   285 }
       
   286 
       
   287 /*!
       
   288 	Slot called when the theme preview view is closed.
       
   289 */
       
   290 void CpThemeControl::previewClosed()
       
   291 {
       
   292     //The theme preview closed, go back
       
   293     //to theme list view.
       
   294     HbMainWindow*  mainWindow = ::mainWindow();
       
   295 	mainWindow->removeView(mThemePreview);
       
   296     mThemePreview->deleteLater();
       
   297     mThemePreview = 0;
       
   298   
       
   299     //reset the current index to active theme, so that the selection remains on current
       
   300     //theme even though another list item is selected.
       
   301     setActiveThemeIndex();
       
   302 	mainWindow->setCurrentView(mThemeListView);   
       
   303 }
   216 }
   304 
   217 
   305 /*!
   218 /*!
   306     Slot for when the theme list view is closed. Ownership of the theme list was given to
   219     Slot for when the theme list view is closed. Ownership of the theme list was given to
   307     control panel, so the class won't delete it.
   220     control panel, so the class won't delete it.
   308     
   221     
   309 */
   222 */
   310 void CpThemeControl::themeListClosed()
   223 void CpThemeControl::themeListClosed()
   311 {
   224 {
   312     mThemeListView = 0;
   225     mThemeListView = 0;
   313 
       
   314     delete mThemePreview;
       
   315     mThemePreview = 0;
       
   316 }
   226 }
   317 
   227 
   318 /*!
   228 /*!
   319     asks the theme list view to close.  
   229     asks the theme list view to close.  
   320 */
   230 */
   329     //so revert back the application priority to normal
   239     //so revert back the application priority to normal
   330     //and go back to control panel view.
   240     //and go back to control panel view.
   331     if(mWaitDialog && mWaitDialog->isVisible()) {
   241     if(mWaitDialog && mWaitDialog->isVisible()) {
   332         mWaitDialog->hide();
   242         mWaitDialog->hide();
   333     }
   243     }
   334     
   244     setActiveThemeIndex();
   335     previewClosed();
       
   336     //ask the themelistview to close.  Control Panel will
       
   337     //take care of removing it from window.
       
   338     triggerThemeListClose();
       
   339     
       
   340     QThread::currentThread()->setPriority(QThread::NormalPriority); 
   245     QThread::currentThread()->setPriority(QThread::NormalPriority); 
   341 }
   246 }
   342 
   247 
   343 void CpThemeControl::themeWaitTimeout()
   248 void CpThemeControl::themeWaitTimeout()
   344 {
   249 {
   345     //If after this timeOut, theme change is still in progress,
   250     //If after this timeOut, theme change is still in progress,
   346     //show a processing dialog.
   251     //show a processing dialog.
   347     if(!mThemeChangeFinished)
   252     if(!mThemeChangeFinished){
   348     {
       
   349         if(!mWaitDialog) {
   253         if(!mWaitDialog) {
   350             mWaitDialog = new HbDialog();
   254             mWaitDialog = new HbDialog();
   351             mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
   255             mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
   352             mWaitDialog->setModal(false);
   256             mWaitDialog->setModal(false);
   353             mWaitDialog->setTimeout(HbPopup::NoTimeout);
   257             mWaitDialog->setTimeout(HbPopup::NoTimeout);
   354             //TODO: need localized text for Hb Dialog
       
   355             // Create and set HbLabel as content widget.
   258             // Create and set HbLabel as content widget.
   356             HbLabel *label = new HbLabel("Processing ...");
   259             QString processingText = hbTrId("txt_common_info_processing") + QString("...");
       
   260             HbLabel *label = new HbLabel(processingText);
   357             label->setAlignment(Qt::AlignCenter);
   261             label->setAlignment(Qt::AlignCenter);
   358             mWaitDialog->setContentWidget(label);
   262             mWaitDialog->setContentWidget(label);
   359         }
   263         }
   360        // as we do not need any signals, calling show() instead of open()
   264        // as we do not need any signals, calling show() instead of open()
   361        mWaitDialog->show();
   265        mWaitDialog->show();
   387     if(themeListModel && currentTheme) {
   291     if(themeListModel && currentTheme) {
   388         QModelIndex sourceIndex = mListModel->index(themeListModel->indexOf(*currentTheme),0);
   292         QModelIndex sourceIndex = mListModel->index(themeListModel->indexOf(*currentTheme),0);
   389         //set current index.
   293         //set current index.
   390         mThemeListView->themeList()->setCurrentIndex(sourceIndex, QItemSelectionModel::SelectCurrent);
   294         mThemeListView->themeList()->setCurrentIndex(sourceIndex, QItemSelectionModel::SelectCurrent);
   391     }
   295     }
   392 }
   296     else {
   393     
   297         mThemeListView->themeList()->setCurrentIndex(QModelIndex(), QItemSelectionModel::Clear);
   394 
   298     }
   395 
   299 }
   396 
   300     
   397 
   301 
   398 
   302 
       
   303 
       
   304 
       
   305