X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=widgets%2Flistwindow.cpp;h=3443024ec9fad339f35d5fe6ccc6e74f6ea95f4f;hb=3d0659ab675c4cadfa6cec67123512f25b380531;hp=9317aa676725e828bbecc0bd78a2ae5d29770c1a;hpb=3ce29c18fb643d34b86baab846bce2804479db48;p=dorian diff --git a/widgets/listwindow.cpp b/widgets/listwindow.cpp index 9317aa6..3443024 100644 --- a/widgets/listwindow.cpp +++ b/widgets/listwindow.cpp @@ -1,171 +1,188 @@ #include +#include +#include #include "listwindow.h" #include "trace.h" -#include "listview.h" +#include "platform.h" -ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0) +#ifdef Q_OS_SYMBIAN +#include "flickcharm.h" +#endif + +ListWindow::ListWindow(const QString &noItems_, QWidget *parent): + QMainWindow(parent), mModel(0), noItems(noItems_) { -#ifdef Q_WS_MAEMO_5 +#if defined(Q_WS_MAEMO_5) setAttribute(Qt::WA_Maemo5StackedWindow, true); - popup = new QMenu(this); - - QScrollArea *scroller = new QScrollArea(this); - setCentralWidget(scroller); - scroller->setProperty("FingerScrollable", true); - // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - scroller->setFrameStyle(QFrame::NoFrame); - scroller->show(); - - QWidget *content = new QWidget(scroller); - contentLayout = new QVBoxLayout(content); - contentLayout->setMargin(0); - content->setLayout(contentLayout); - content->show(); - - scroller->setWidget(content); - scroller->setWidgetResizable(true); -#else - QFrame *frame = new QFrame(this); - setCentralWidget(frame); - contentLayout = new QHBoxLayout(); - frame->setLayout(contentLayout); -#ifndef Q_OS_SYMBIAN - buttonBox = new QDialogButtonBox(Qt::Vertical, this); - contentLayout->addWidget(buttonBox); +#endif + setAttribute(Qt::WA_DeleteOnClose); + + list = new QListWidget(this); + list->setSelectionMode(QAbstractItemView::SingleSelection); +#if defined(Q_OS_SYMBIAN) + list->setFixedWidth(QApplication::desktop()->availableGeometry().width()); + list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); +#endif + populateList(); + setCentralWidget(list); + +#ifdef Q_OS_SYMBIAN + charm = new FlickCharm(this); + // charm->activateOn(list); + QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this); + closeAction->setSoftKeyRole(QAction::NegativeSoftKey); + connect(closeAction, SIGNAL(triggered()), this, SLOT(close())); + QMainWindow::addAction(closeAction); #endif // Q_OS_SYMBIAN -#endif // Q_WS_MAEMO_5 + + connect(list, SIGNAL(activated(const QModelIndex &)), + this, SLOT(onItemActivated(const QModelIndex &))); } -void ListWindow::addList(ListView *listView) +void ListWindow::populateList() { - Trace t("ListWindow::addList"); - list = listView; -#ifdef Q_WS_MAEMO_5 - list->installEventFilter(this); - list->setMinimumHeight(list->contentsHeight()); - contentLayout->addWidget(list); - connect(list->model(), - SIGNAL(rowsInserted(const QModelIndex &, int, int)), - this, SLOT(onModelChanged())); - connect(list->model(), - SIGNAL(rowsRemoved(const QModelIndex &, int, int)), - this, SLOT(onModelChanged())); -#else - contentLayout->insertWidget(0, list); -#endif - connect(list->selectionModel(), - SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), - this, - SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &))); + TRACE; + + list->clear(); + list->setIconSize(QSize(48, 48)); // FIXME + list->setUniformItemSizes(true); + if (mModel && mModel->rowCount()) { + for (int i = 0; i < mModel->rowCount(); i++) { + QModelIndex index = mModel->index(i, 0); + QString text = mModel->data(index, Qt::DisplayRole).toString(); + QVariant imageData = mModel->data(index, Qt::DecorationRole); + QIcon icon(QPixmap::fromImage(imageData.value())); + (void)new QListWidgetItem(icon, text, list); + } + } else { + QListWidgetItem *item = new QListWidgetItem(noItems); + item->setFlags(Qt::NoItemFlags); + list->addItem(item); + } + for (int i = 0; i < buttons.count(); i++) { + insertButton(i, buttons[i]); + } } -void ListWindow::addAction(const QString &title, QObject *receiver, - const char *slot, const QString &iconPath, - QDialogButtonBox::ButtonRole role) +void ListWindow::insertButton(int row, const Button &b) { - Trace t("ListWindow::addAction"); -#ifdef Q_WS_MAEMO_5 - Q_UNUSED(role); - QPushButton *button = new QPushButton(QIcon(iconPath), title, this); - contentLayout->addWidget(button); - connect(button, SIGNAL(clicked()), receiver, slot); -#elif defined(Q_OS_SYMBIAN) - Q_UNUSED(role); - QAction *action = new QAction(title, this); - connect(action, SIGNAL(triggered()), receiver, slot); - action->setSoftKeyRole(QAction::PositiveSoftKey); - menuBar()->addAction(action); -#else - Q_UNUSED(iconPath); - QPushButton *button = buttonBox->addButton(title, role); - connect(button, SIGNAL(clicked()), receiver, slot); -#endif // Q_WS_MAEMO_5 + QPushButton *pushButton = new QPushButton( + QIcon(Platform::instance()->icon(b.iconName)), b.title, this); + pushButton->setFixedWidth(list->width()); + connect(pushButton, SIGNAL(clicked()), b.receiver, b.slot); + QListWidgetItem *item = new QListWidgetItem(); + item->setFlags(Qt::NoItemFlags); + list->insertItem(row, item); + list->setItemWidget(item, pushButton); } -void ListWindow::addItemAction(const QString &title, QObject *receiver, - const char *slot) +void ListWindow::setModel(QAbstractItemModel *aModel) { - Trace t("ListWindow::addItemAction"); -#ifdef Q_WS_MAEMO - popup->addAction(title, receiver, slot); -#elif defined Q_OS_SYMBIAN - QAction *action = new QAction(title, this); - connect(action, SIGNAL(triggered()), receiver, slot); - action->setSoftKeyRole(QAction::PositiveSoftKey); - menuBar()->addAction(action); - // FIXME: Add action to the list of item specific actions -#else - QPushButton *button = - buttonBox->addButton(title, QDialogButtonBox::ActionRole); - connect(button, SIGNAL(clicked()), receiver, slot); - itemButtons.append(button); - activateItemButtons(); -#endif // Q_WS_MAEMO_5 + TRACE; + mModel = aModel; + populateList(); + if (mModel) { + connect(mModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), + this, SLOT(populateList())); + connect(mModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), + this, SLOT(populateList())); + connect(mModel, SIGNAL(rowsInserted(QModelIndex, int, int)), + this, SLOT(populateList())); + connect(mModel, SIGNAL(layoutChanged()), this, SLOT(populateList())); + } } -#ifdef Q_WS_MAEMO_5 - -void ListWindow::closeEvent(QCloseEvent *event) +QAbstractItemModel *ListWindow::model() const { - // Work around Maemo/Qt but: Menu items are not removed on close - menuBar()->clear(); - event->accept(); + return mModel; } -#endif // Q_WS_MAEMO_5 - -void ListWindow::onSelectionChanged(const QItemSelection &selected, - const QItemSelection &deselected) +void ListWindow::addButton(const QString &title, QObject *receiver, + const char *slot, const QString &iconName) { - Q_UNUSED(selected); - Q_UNUSED(deselected); -#ifndef Q_WS_MAEMO_5 - activateItemButtons(); + TRACE; + +#ifdef Q_OS_SYMBIAN + Q_UNUSED(iconName); + addMenuAction(title, receiver, slot); +#else + Button b; + b.title = title; + b.receiver = receiver; + b.slot = slot; + b.iconName = iconName; + insertButton(buttons.length(), b); + buttons.append(b); #endif } -#ifndef Q_WS_MAEMO_5 +QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver, + const char *slot) +{ + TRACE; + QAction *action = 0; +#if defined(Q_WS_MAEMO_5) + action = menuBar()->addAction(title); + connect(action, SIGNAL(triggered()), receiver, slot); +#elif defined(Q_OS_SYMBIAN) + action = new QAction(title, this); + connect(action, SIGNAL(triggered()), receiver, slot); + action->setSoftKeyRole(QAction::PositiveSoftKey); + menuBar()->addAction(action); +#else + Q_UNUSED(title); + Q_UNUSED(receiver); + Q_UNUSED(slot); + action = new QAction(this); +#endif + action->setCheckable(true); + return action; +} -void ListWindow::activateItemButtons() +void ListWindow::onItemActivated(const QModelIndex &index) { - bool enable = false; - if (list) { - enable = list->selectionModel()->hasSelection(); + TRACE; + + // Work around Qt/Symbian^3 bug: Disabled list items still can be selected + if (!mModel) { + return; } - foreach (QPushButton *button, itemButtons) { - button->setEnabled(enable); + if (!mModel->rowCount()) { + return; } -} -#endif // ! Q_WS_MAEMO_5 + int row = index.row() - buttons.count(); + qDebug() << "Activated" << index.row() << ", emit activated(" << row << ")"; + emit activated(mModel->index(row, 0)); +} -#ifdef Q_WS_MAEMO_5 +void ListWindow::setCurrentItem(const QModelIndex &item) +{ + int index = item.row(); + list->setCurrentItem(list->item(index + buttons.count())); +} -bool ListWindow::eventFilter(QObject *obj, QEvent *event) +QModelIndex ListWindow::currentItem() const { - if (event->type() == QEvent::ContextMenu) { - qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu"; - if (popup->actions().size()) { - QMouseEvent *mouseEvent = static_cast (event); - QPoint pos = mouseEvent->globalPos(); - pos.setX(pos.x() - 150); - if (pos.x() < 0) { - pos.setX(0); - } - popup->exec(pos); - } - return true; - } else { - return QObject::eventFilter(obj, event); + TRACE; + QListWidgetItem *currentItem = list->currentItem(); + if (currentItem) { + int row = list->row(currentItem) - buttons.count(); + qDebug() << "Current row is" << row; + return mModel->index(row, 0); } + return QModelIndex(); } -void ListWindow::onModelChanged() +#ifdef Q_WS_MAEMO_5 + +void ListWindow::closeEvent(QCloseEvent *event) { - qDebug() << "ListWindow::onModelChanged"; - list->setMinimumHeight(list->contentsHeight()); + // Work around Maemo/Qt bug: Menu items are not removed on close + menuBar()->clear(); + event->accept(); + QMainWindow::closeEvent(event); } #endif // Q_WS_MAEMO_5 @@ -174,6 +191,9 @@ void ListWindow::onModelChanged() void ListWindow::show() { + foreach (QWidget *w, QApplication::allWidgets()) { + w->setContextMenuPolicy(Qt::NoContextMenu); + } showMaximized(); }