File: ingredientview.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (93 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/***************************************************************************
  ingredientview.cpp
  -------------------
  Ingredient view
  -------------------
  Copyright 2006-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QAction>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QMenu>

#include "ingredientview.h"

///////////////////////////////////////////////////////////////////////////////
// IngredientView                                                            //
///////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// IngredientView()
// ------------
// Constructor

IngredientView::IngredientView(QWidget *parent)
    : QTableView(parent), addaction_(0), removeaction_(0)
{
    setSortingEnabled(true);

    // create context menu actions
    addaction_ = new QAction(tr("&Add Ingredient"), this);
    addaction_->setShortcut(QKeySequence(tr("Ctrl+Insert")));
    addaction_->setStatusTip(tr("Add a new ingredient"));
    connect(addaction_, SIGNAL(triggered()), this, SLOT(addIngredient()));

    removeaction_ = new QAction(tr("&Remove Ingredient"), this);
    removeaction_->setShortcut(QKeySequence(tr("Ctrl+Delete")));
    removeaction_->setStatusTip(tr("Remove selected ingredient"));
    connect(removeaction_, SIGNAL(triggered()), this, SLOT(removeIngredient()));

    clearaction_ = new QAction(tr("&Clear Selection"), this);
    clearaction_->setShortcut(QKeySequence(tr("Escape")));
    clearaction_->setStatusTip(tr("Clear selection"));
    connect(clearaction_, SIGNAL(triggered()), this, SLOT(clearSelection()));

    addAction(addaction_);
    addAction(removeaction_);
    addAction(clearaction_);
}

IngredientView::~IngredientView()
{
}

//////////////////////////////////////////////////////////////////////////////
// contextMenuEvent()
// ------------------
// Create and show context menu

void IngredientView::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);
    menu.addAction(addaction_);
    menu.addAction(removeaction_);
    menu.addSeparator();
    menu.addAction(clearaction_);
    menu.exec(event->globalPos());
}

//////////////////////////////////////////////////////////////////////////////
// addIngredient()
// ---------------
// Add a new blank ingredient

void IngredientView::addIngredient()
{
    model()->insertRows(currentIndex().row(), 1, QModelIndex());
}

//////////////////////////////////////////////////////////////////////////////
// removeIngredient()
// ------------------
// Remove the selected ingredient

void IngredientView::removeIngredient()
{
    // can only remove valid indexes
    if (currentIndex().isValid()) {
        model()->removeRows(currentIndex().row(), 1, QModelIndex());
        clearSelection();
    }
}