src/hbcore/gui/hbviewactionmanager.cpp
changeset 5 627c4a0fd0e7
parent 0 16d8024aca5e
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    34 /*!
    34 /*!
    35     @proto
    35     @proto
    36     @hbcore
    36     @hbcore
    37     \class HbViewActionManager
    37     \class HbViewActionManager
    38     \brief HbViewActionManager is the class that decides switch to container
    38     \brief HbViewActionManager is the class that decides switch to container
    39     the action is placed (options menu or toolbar). 
    39     the action is placed (options menu or toolbar).
    40     Decision is based the UI commands distribution guide that is read
    40     Decision is based the UI commands distribution guide that is read
    41     automatically.
    41     automatically.
    42 
    42 
    43     \internal
    43     \internal
    44 */
    44 */
    45 
    45 
    46 /*! 
    46 /*!
    47     Constructor.
    47     Constructor.
    48 */
    48 */
    49 HbViewActionManager::HbViewActionManager(HbView *view) :
    49 HbViewActionManager::HbViewActionManager(HbView *view) :
    50     QObject(view), view(view), toolBarMaxCount(99), 
    50     QObject(view), view(view), toolBarMaxCount(99),
    51     defaultContainer(HbView::OptionsMenu), orientation(Qt::Vertical)
    51     defaultContainer(HbView::OptionsMenu), orientation(Qt::Vertical)
    52 {
    52 {
    53     if (view) {
    53     if (view) {
    54         HbMainWindow* mainWin = view->mainWindow();
    54         HbMainWindow *mainWin = view->mainWindow();
    55         if (mainWin) {
    55         if (mainWin) {
    56             connect(mainWin, SIGNAL(orientationChanged(Qt::Orientation)),
    56             connect(mainWin, SIGNAL(orientationChanged(Qt::Orientation)),
    57                     this, SLOT(orientationChanged(Qt::Orientation)));
    57                     this, SLOT(orientationChanged(Qt::Orientation)));
    58             orientation = mainWin->orientation();
    58             orientation = mainWin->orientation();
    59         }
    59         }
    60     }
    60     }
    61 
    61 
    62     createTemplate();
    62     createTemplate();
    63 }
    63 }
    64 
    64 
    65 /*! 
    65 /*!
    66     Destructor.
    66     Destructor.
    67 */
    67 */
    68 HbViewActionManager::~HbViewActionManager()
    68 HbViewActionManager::~HbViewActionManager()
    69 {
    69 {
    70 
    70 
    71 }
    71 }
    72 
    72 
    73 /*! 
    73 /*!
    74     The function adds \a action to either menu's or toolbar's list of actions.
    74     The function adds \a action to either menu's or toolbar's list of actions.
    75     The ownership of \a action is not transferred.
    75     The ownership of \a action is not transferred.
    76 
    76 
    77     \sa removeAction
    77     \sa removeAction
    78 */
    78 */
    83 
    83 
    84     // Find the right container from the guide
    84     // Find the right container from the guide
    85     if (preferredContainer == HbView::NotSpecified) {
    85     if (preferredContainer == HbView::NotSpecified) {
    86         container = actualContainer(action, preferredContainer);
    86         container = actualContainer(action, preferredContainer);
    87     }
    87     }
    88  
    88 
    89     // Add action to right container
    89     // Add action to right container
    90     if (container == HbView::OptionsMenu) {
    90     if (container == HbView::OptionsMenu) {
    91         view->menu()->addAction(action);    
    91         view->menu()->addAction(action);
    92     }
    92     } else if (container == HbView::ToolBar) {
    93     else if (container == HbView::ToolBar) {
       
    94         view->toolBar()->addAction(action);
    93         view->toolBar()->addAction(action);
    95         if (view->toolBar()->actions().count() > toolBarMaxCount) {
    94         if (view->toolBar()->actions().count() > toolBarMaxCount) {
    96            moveActionToMenu(); 
    95             moveActionToMenu();
    97         }
    96         }
    98     }
    97     }
    99 
    98 
   100     // Insert data to private structure
    99     // Insert data to private structure
   101     Placement placement(container, preferredContainer);
   100     Placement placement(container, preferredContainer);
   102     distributedActions.insert(action, placement);
   101     distributedActions.insert(action, placement);
   103 }
   102 }
   104 
   103 
   105 /*!
   104 /*!
   106     The function removes \a action either menu's or toolbar's list of actions.
   105     The function removes \a action either menu's or toolbar's list of actions.
   107     
   106 
   108     The ownership of \a action is not transferred.
   107     The ownership of \a action is not transferred.
   109  */
   108  */
   110 void HbViewActionManager::removeAction(QAction *action)
   109 void HbViewActionManager::removeAction(QAction *action)
   111 {
   110 {
   112     removeAction(action, true);
   111     removeAction(action, true);
   113 }
   112 }
   114 
   113 
   115 /*!
   114 /*!
   116     The function replaces an old \a action either menu's or 
   115     The function replaces an old \a action either menu's or
   117     toolbar's list of actions.
   116     toolbar's list of actions.
   118     
   117 
   119     The ownership of \a action is transferred to the caller.
   118     The ownership of \a action is transferred to the caller.
   120  */
   119  */
   121 void HbViewActionManager::replaceAction(QAction *oldAction, QAction *newAction)
   120 void HbViewActionManager::replaceAction(QAction *oldAction, QAction *newAction)
   122 {
   121 {
   123     QMap<QAction*, Placement>::const_iterator i =
   122     QMap<QAction *, Placement>::const_iterator i =
   124         distributedActions.find(oldAction);
   123         distributedActions.find(oldAction);
   125     if (i != distributedActions.end()) {
   124     if (i != distributedActions.end()) {
   126         HbViewActionManager::Placement placement = i.value();
   125         HbViewActionManager::Placement placement = i.value();
   127         QList<QAction *> list = containerActions(placement.container);
   126         QList<QAction *> list = containerActions(placement.container);
   128         int index = list.indexOf(oldAction);
   127         int index = list.indexOf(oldAction);
   129         if (index >= 0) {
   128         if (index >= 0) {
   130             if (placement.container == HbView::OptionsMenu) {
   129             if (placement.container == HbView::OptionsMenu) {
   131                 view->menu()->insertAction(oldAction, newAction);
   130                 view->menu()->insertAction(oldAction, newAction);
   132                 view->menu()->removeAction(oldAction);
   131                 view->menu()->removeAction(oldAction);
   133             }  
   132             } else if (placement.container == HbView::ToolBar) {
   134             else if (placement.container == HbView::ToolBar) {
       
   135                 view->toolBar()->insertAction(oldAction, newAction);
   133                 view->toolBar()->insertAction(oldAction, newAction);
   136                 view->toolBar()->removeAction(oldAction);
   134                 view->toolBar()->removeAction(oldAction);
   137             }
   135             }
   138             // Update private structure
   136             // Update private structure
   139             distributedActions.remove(oldAction);
   137             distributedActions.remove(oldAction);
   169  */
   167  */
   170 void HbViewActionManager::setDefaultContainer(const QString &containerString)
   168 void HbViewActionManager::setDefaultContainer(const QString &containerString)
   171 {
   169 {
   172     if (containerString == "ToolBar") {
   170     if (containerString == "ToolBar") {
   173         defaultContainer = HbView::ToolBar;
   171         defaultContainer = HbView::ToolBar;
   174     }
   172     } else if (containerString == "OptionsMenu") {
   175     else if (containerString == "OptionsMenu") {
       
   176         defaultContainer = HbView::OptionsMenu;
   173         defaultContainer = HbView::OptionsMenu;
   177     }
   174     } else {
   178     else {
       
   179         defaultContainer = HbView::NotSpecified;
   175         defaultContainer = HbView::NotSpecified;
   180     }  
   176     }
   181 }
   177 }
   182 
   178 
   183 /*!
   179 /*!
   184     Adds a new item to the distribution guide. Used by HbActionManagerXmlParser.
   180     Adds a new item to the distribution guide. Used by HbActionManagerXmlParser.
   185 
   181 
   186     \internal
   182     \internal
   187  */
   183  */
   188 void HbViewActionManager::addGuideItem(const GuideItem &guideItem)
   184 void HbViewActionManager::addGuideItem(const GuideItem &guideItem)
   189 {
   185 {
   190     distributionGuide.append(guideItem);   
   186     distributionGuide.append(guideItem);
   191 }
   187 }
   192 
   188 
   193 /*!
   189 /*!
   194    This signal is emitted when orientation change is done.
   190    This signal is emitted when orientation change is done.
   195 
   191 
   202     // clear the current guide and create it again
   198     // clear the current guide and create it again
   203     distributionGuide.clear();
   199     distributionGuide.clear();
   204     createTemplate();
   200     createTemplate();
   205 
   201 
   206     // Remove actions from containers
   202     // Remove actions from containers
   207     QMapIterator<QAction*, Placement> i(distributedActions);
   203     QMapIterator<QAction *, Placement> i(distributedActions);
   208     while (i.hasNext()) {
   204     while (i.hasNext()) {
   209         i.next();
   205         i.next();
   210         removeAction(i.key(), false);
   206         removeAction(i.key(), false);
   211     }
   207     }
   212     // Add them again with the new guide
   208     // Add them again with the new guide
   221 {
   217 {
   222     QFile file;
   218     QFile file;
   223     QString path(":/actionmanager/");
   219     QString path(":/actionmanager/");
   224     if (orientation == Qt::Vertical) {
   220     if (orientation == Qt::Vertical) {
   225         file.setFileName(path + "distribution_guide_vertical.xml");
   221         file.setFileName(path + "distribution_guide_vertical.xml");
   226     }
   222     } else {
   227     else {
       
   228         file.setFileName(path + "distribution_guide_horizontal.xml");
   223         file.setFileName(path + "distribution_guide_horizontal.xml");
   229     }
   224     }
   230  
   225 
   231     if (!file.open(QIODevice::ReadOnly)) {
   226     if (!file.open(QIODevice::ReadOnly)) {
   232         qWarning("HbViewActionManager::createTemplate: opening file failed");
   227         qWarning("HbViewActionManager::createTemplate: opening file failed");
   233      } 
   228     } else {
   234     else {
       
   235         HbActionManagerXmlParser reader(this);
   229         HbActionManagerXmlParser reader(this);
   236         if (!reader.read(&file)) {
   230         if (!reader.read(&file)) {
   237             qWarning("HbViewActionManager::createTemplate: reading file failed");
   231             qWarning("HbViewActionManager::createTemplate: reading file failed");
   238         }        
   232         }
   239     }
   233     }
   240 }
   234 }
   241 
   235 
   242 HbView::ActionContainer HbViewActionManager::actualContainer(QAction *action,
   236 HbView::ActionContainer HbViewActionManager::actualContainer(QAction *action,
   243     HbView::ActionContainer preferredContainer)
   237         HbView::ActionContainer preferredContainer)
   244 {
   238 {
   245     HbView::ActionContainer container(preferredContainer);
   239     HbView::ActionContainer container(preferredContainer);
   246 
   240 
   247     HbAction *hbAction = qobject_cast<HbAction *>(action);
   241     HbAction *hbAction = qobject_cast<HbAction *>(action);
   248     // use the default container, if the downcast failed cause then
   242     // use the default container, if the downcast failed cause then
   249     // we are not able to obtain the commandRole from the action
   243     // we are not able to obtain the commandRole from the action
   250     if(!hbAction){
   244     if (!hbAction) {
   251         return defaultContainer;
   245         return defaultContainer;
   252     }
   246     }
   253 
   247 
   254     // Find the container from the distribution guide
   248     // Find the container from the distribution guide
   255     HbViewActionManager::GuideItem guideItem;
   249     HbViewActionManager::GuideItem guideItem;
   279     view->toolBar()->removeAction(action);
   273     view->toolBar()->removeAction(action);
   280     distributedActions.remove(action);
   274     distributedActions.remove(action);
   281 
   275 
   282     // Add actions to menu if it does not exist already
   276     // Add actions to menu if it does not exist already
   283     if (view->menu()->actions().indexOf(action) < 0) {
   277     if (view->menu()->actions().indexOf(action) < 0) {
   284         view->menu()->addAction(action); 
   278         view->menu()->addAction(action);
   285         // Update private structure
   279         // Update private structure
   286         Placement placement(HbView::OptionsMenu, HbView::ToolBar);
   280         Placement placement(HbView::OptionsMenu, HbView::ToolBar);
   287         distributedActions.insert(action, placement);
   281         distributedActions.insert(action, placement);
   288     }
   282     }
   289 }
   283 }
   290 
   284 
   291 void HbViewActionManager::removeAction(QAction *action, bool removeFromMap)
   285 void HbViewActionManager::removeAction(QAction *action, bool removeFromMap)
   292 {
   286 {
   293     QMap<QAction*, Placement>::const_iterator i =
   287     QMap<QAction *, Placement>::const_iterator i =
   294         distributedActions.find(action);
   288         distributedActions.find(action);
   295     if (i != distributedActions.end()) {
   289     if (i != distributedActions.end()) {
   296         // Remove from the container
   290         // Remove from the container
   297         HbViewActionManager::Placement placement = i.value();
   291         HbViewActionManager::Placement placement = i.value();
   298         if (placement.container == HbView::OptionsMenu) {
   292         if (placement.container == HbView::OptionsMenu) {
   299             view->menu()->removeAction(action);    
   293             view->menu()->removeAction(action);
   300         }
   294         } else if (placement.container == HbView::ToolBar) {
   301         else if (placement.container == HbView::ToolBar) {
       
   302             view->toolBar()->removeAction(action);
   295             view->toolBar()->removeAction(action);
   303         }
   296         }
   304         
   297 
   305         if (removeFromMap) {
   298         if (removeFromMap) {
   306             // Remove from private structure
   299             // Remove from private structure
   307             distributedActions.remove(action);
   300             distributedActions.remove(action);
   308         }
   301         }
   309     }
   302     }