src/hbplugins/devicedialogs/indicatormenuplugin/hbindicatormenucontent.cpp
changeset 34 ed14f46c0e55
parent 5 627c4a0fd0e7
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
    26 #include <hbindicatorinterface.h>
    26 #include <hbindicatorinterface.h>
       
    27 #include <hbmainwindow.h>
    27 #include "hbindicatormenucontent_p.h"
    28 #include "hbindicatormenucontent_p.h"
    28 
    29 
    29 static const int ListWidgetItemIndicatorTypeRole = Hb::UserRole;
    30 static const int ListWidgetItemIndicatorTypeRole = Hb::UserRole;
    30 static const int DefaultItemsVisible = 6;
       
    31 
    31 
    32 HbIndicatorListItem::HbIndicatorListItem(QGraphicsItem *parent) :
    32 HbIndicatorListItem::HbIndicatorListItem(QGraphicsItem *parent) :
    33         HbListViewItem(parent)
    33         HbListViewItem(parent)
    34 {
    34 {
    35     setObjectName("indicatorListItem");
    35     setObjectName("indicatorListItem");
    42     item->setContentsMargins(0,0,0,0);
    42     item->setContentsMargins(0,0,0,0);
    43     return item;
    43     return item;
    44 }
    44 }
    45 
    45 
    46 IndicatorList::IndicatorList(HbIndicatorMenuContent *content) :
    46 IndicatorList::IndicatorList(HbIndicatorMenuContent *content) :
    47         HbListView(content), mUpdateListSize(false)
    47     HbListView(content), mUpdateListSize(false), mPreferredHeight(-1), mContent(content)
    48 {
    48 {
    49     setItemRecycling(true);
    49     setItemRecycling(true);
    50     setUniformItemSizes(true);
    50     setUniformItemSizes(true);
       
    51 
       
    52     qreal text1(0);
       
    53     style()->parameter(QLatin1String("hb-param-text-height-primary"), text1);
       
    54     qreal text2(0);
       
    55     style()->parameter(QLatin1String("hb-param-text-height-secondary"), text2);
       
    56     qreal margin(0);
       
    57     style()->parameter(QLatin1String("hb-param-margin-gene-middle-horizontal"), margin);
       
    58     mPreferredHeight = text1 + text2 + 3 * margin;
    51 }
    59 }
    52 
    60 
    53 void IndicatorList::rowsInserted(
    61 void IndicatorList::rowsInserted(
    54         const QModelIndex &parent, int start, int end)
    62         const QModelIndex &parent, int start, int end)
    55 {
    63 {
    92             //get the height of one of the listitems.
   100             //get the height of one of the listitems.
    93             for (int i = 0; i < indicators && !item; ++i) {
   101             for (int i = 0; i < indicators && !item; ++i) {
    94                 item = viewItem(i);
   102                 item = viewItem(i);
    95             }
   103             }
    96             if (item) {
   104             if (item) {
    97                 QSizeF itemSize(item->effectiveSizeHint(Qt::PreferredSize));
   105                 QSizeF itemSize(item->effectiveSizeHint(Qt::PreferredSize, QSizeF(-1, mPreferredHeight)));
    98                 qreal height = qMin(DefaultItemsVisible, indicators)
   106                 int defaultItemsVisible = 6;
       
   107                 if (mContent->orientation() == Qt::Horizontal) {
       
   108                     defaultItemsVisible = 4;
       
   109                 } 
       
   110                 qreal height = qMin(defaultItemsVisible, indicators)
    99                                * itemSize.height();
   111                                * itemSize.height();
   100                 mSize = QSizeF(itemSize.width(), height);
   112                 mSize = QSizeF(itemSize.width(), height);
   101             } else {
   113             } else {
   102                 mSize.setWidth(0);
   114                 mSize.setWidth(0);
   103                 mSize.setHeight(0);
   115                 mSize.setHeight(0);
   122 
   134 
   123 /*!
   135 /*!
   124     Constructs a new HbItemContainer with \a parent.
   136     Constructs a new HbItemContainer with \a parent.
   125 */
   137 */
   126 HbIndicatorMenuContent::HbIndicatorMenuContent(QGraphicsItem *parent) :
   138 HbIndicatorMenuContent::HbIndicatorMenuContent(QGraphicsItem *parent) :
   127     HbWidget(parent)
   139     HbWidget(parent), mOrientation(Qt::Vertical)
   128 {
   140 {
   129     for (int i = 0; i < IndicatorTypes; ++i) {
   141     for (int i = 0; i < IndicatorTypes; ++i) {
   130         mGroupTypeIndeces[i] = 0;
   142         mGroupTypeIndeces[i] = 0;
   131     }
   143     }
   132 
   144 
   150 int HbIndicatorMenuContent::indicatorCount() const
   162 int HbIndicatorMenuContent::indicatorCount() const
   151 {
   163 {
   152      return indicatorModel.rowCount();
   164      return indicatorModel.rowCount();
   153 }
   165 }
   154 
   166 
   155 void HbIndicatorMenuContent::handleAboutToShow()
   167 void HbIndicatorMenuContent::handleAboutToShow(HbMainWindow *mainWindow)
   156 {
   168 {
       
   169     if (mainWindow) {
       
   170         mOrientation = mainWindow->orientation();
       
   171     }
       
   172 
   157     for (int i = 0; i < mIndicatorList->model()->rowCount(); ++i) {
   173     for (int i = 0; i < mIndicatorList->model()->rowCount(); ++i) {
   158         HbIndicatorInterface *indicator =
   174         HbIndicatorInterface *indicator =
   159             indicatorFromIndex(indicatorModel.item(i)->index());
   175             indicatorFromIndex(indicatorModel.item(i)->index());
   160         if (indicator) {
   176         if (indicator) {
   161             if (indicator->refreshData()) {
   177             bool refreshed = true;
       
   178             try {
       
   179                 refreshed = indicator->refreshData();
       
   180             } catch (const std::bad_alloc &) {
       
   181                 refreshed = false;
       
   182             }
       
   183             if (refreshed) {
   162                 setData(indicator, indicatorModel.item(i)->index());
   184                 setData(indicator, indicatorModel.item(i)->index());
   163             }
   185             }
   164         }
   186         }
   165     }
   187     }
       
   188 }
       
   189 
       
   190 Qt::Orientation HbIndicatorMenuContent::orientation() const
       
   191 {
       
   192     return mOrientation;
   166 }
   193 }
   167 
   194 
   168 void HbIndicatorMenuContent::updatePrimitives()
   195 void HbIndicatorMenuContent::updatePrimitives()
   169 {
   196 {
   170     repolish();
   197     repolish();
   173 void HbIndicatorMenuContent::itemActivated(const QModelIndex &modelIndex)
   200 void HbIndicatorMenuContent::itemActivated(const QModelIndex &modelIndex)
   174 {
   201 {
   175     HbIndicatorInterface *indicator = indicatorFromIndex(modelIndex);
   202     HbIndicatorInterface *indicator = indicatorFromIndex(modelIndex);
   176     if (indicator && indicator->interactionTypes().testFlag(
   203     if (indicator && indicator->interactionTypes().testFlag(
   177             HbIndicatorInterface::InteractionActivated)) {
   204             HbIndicatorInterface::InteractionActivated)) {
   178         indicator->handleInteraction(HbIndicatorInterface::InteractionActivated);
   205         try {
       
   206             indicator->handleInteraction(HbIndicatorInterface::InteractionActivated);
       
   207         }  catch (const std::bad_alloc &) {
       
   208         }
   179         emit aboutToClose();
   209         emit aboutToClose();
   180     }
   210     }
   181 }
   211 }
   182 
   212 
   183 void HbIndicatorMenuContent::indicatorsActivated(
   213 void HbIndicatorMenuContent::indicatorsActivated(
   227         HbIndicatorInterface::SecondaryTextRole).toString();
   257         HbIndicatorInterface::SecondaryTextRole).toString();
   228     QString iconPath = source->indicatorData(
   258     QString iconPath = source->indicatorData(
   229         HbIndicatorInterface::DecorationNameRole).toString();
   259         HbIndicatorInterface::DecorationNameRole).toString();
   230 
   260 
   231     QStringList texts;
   261     QStringList texts;
       
   262 
       
   263     // List doesn't allow empty text
       
   264     static const char empty[] = " ";
       
   265     if (primaryText.isEmpty()) {
       
   266         primaryText.append(empty);
       
   267     }
       
   268     // Always reserve space for secondary text. Item size should be uniform.
   232     if (secondaryText.isEmpty()) {
   269     if (secondaryText.isEmpty()) {
   233         secondaryText = " "; //always reserve space for secondary text. Item size should be uniform.
   270         secondaryText.append(empty);
   234     }
   271     }
   235     texts << primaryText << secondaryText;
   272     texts << primaryText << secondaryText;
   236 
   273 
   237     indicatorModel.setData(modelIndex, texts, Qt::DisplayRole);
   274     indicatorModel.setData(modelIndex, texts, Qt::DisplayRole);
   238     if (!iconPath.isEmpty()) {
   275     if (!iconPath.isEmpty()) {