| 1 | /*************************************************************************** |
| 2 | ingredientview.cpp |
| 3 | ------------------- |
| 4 | Ingredient view |
| 5 | ------------------- |
| 6 | Copyright 2006-2008, David Johnson |
| 7 | Please see the header file for copyright and license information |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QAction> |
| 11 | #include <QContextMenuEvent> |
| 12 | #include <QHeaderView> |
| 13 | #include <QMenu> |
| 14 | |
| 15 | #include "ingredientview.h" |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | // IngredientView // |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | ////////////////////////////////////////////////////////////////////////////// |
| 22 | // IngredientView() |
| 23 | // ------------ |
| 24 | // Constructor |
| 25 | |
| 26 | IngredientView::IngredientView(QWidget *parent) |
| 27 | : QTableView(parent), addaction_(0), removeaction_(0) |
| 28 | { |
| 29 | setSortingEnabled(true); |
| 30 | |
| 31 | // create context menu actions |
| 32 | addaction_ = new QAction(tr("&Add Ingredient"), this); |
| 33 | addaction_->setShortcut(QKeySequence(tr("Ctrl+Insert"))); |
| 34 | addaction_->setStatusTip(tr("Add a new ingredient")); |
| 35 | connect(addaction_, SIGNAL(triggered()), this, SLOT(addIngredient())); |
| 36 | |
| 37 | removeaction_ = new QAction(tr("&Remove Ingredient"), this); |
| 38 | removeaction_->setShortcut(QKeySequence(tr("Ctrl+Delete"))); |
| 39 | removeaction_->setStatusTip(tr("Remove selected ingredient")); |
| 40 | connect(removeaction_, SIGNAL(triggered()), this, SLOT(removeIngredient())); |
| 41 | |
| 42 | clearaction_ = new QAction(tr("&Clear Selection"), this); |
| 43 | clearaction_->setShortcut(QKeySequence(tr("Escape"))); |
| 44 | clearaction_->setStatusTip(tr("Clear selection")); |
| 45 | connect(clearaction_, SIGNAL(triggered()), this, SLOT(clearSelection())); |
| 46 | |
| 47 | addAction(addaction_); |
| 48 | addAction(removeaction_); |
| 49 | addAction(clearaction_); |
| 50 | } |
| 51 | |
| 52 | IngredientView::~IngredientView() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | ////////////////////////////////////////////////////////////////////////////// |
| 57 | // contextMenuEvent() |
| 58 | // ------------------ |
| 59 | // Create and show context menu |
| 60 | |
| 61 | void IngredientView::contextMenuEvent(QContextMenuEvent *event) |
| 62 | { |
| 63 | QMenu menu(this); |
| 64 | menu.addAction(addaction_); |
| 65 | menu.addAction(removeaction_); |
| 66 | menu.addSeparator(); |
| 67 | menu.addAction(clearaction_); |
| 68 | menu.exec(event->globalPos()); |
| 69 | } |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | // addIngredient() |
| 73 | // --------------- |
| 74 | // Add a new blank ingredient |
| 75 | |
| 76 | void IngredientView::addIngredient() |
| 77 | { |
| 78 | model()->insertRows(currentIndex().row(), 1, QModelIndex()); |
| 79 | } |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | // removeIngredient() |
| 83 | // ------------------ |
| 84 | // Remove the selected ingredient |
| 85 | |
| 86 | void IngredientView::removeIngredient() |
| 87 | { |
| 88 | // can only remove valid indexes |
| 89 | if (currentIndex().isValid()) { |
| 90 | model()->removeRows(currentIndex().row(), 1, QModelIndex()); |
| 91 | clearSelection(); |
| 92 | } |
| 93 | } |