Logo Search packages:      
Sourcecode: qbrew version File versions

ingredientview.cpp

/***************************************************************************
  ingredientview.cpp
  -------------------
  Ingredient view
  -------------------
  Copyright 2006-2007, 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)
{
    // create context menu actions
    addaction_ = new QAction("&Add Ingredient", this);
    addaction_->setShortcut(QKeySequence("Ctrl+Insert"));
    addaction_->setStatusTip("Add a new ingredient");
    connect(addaction_, SIGNAL(triggered()), this, SLOT(addIngredient()));

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

    clearaction_ = new QAction("&Clear Selection", this);
    clearaction_->setShortcut(QKeySequence("Escape"));
    clearaction_->setStatusTip("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();
    }
}

//////////////////////////////////////////////////////////////////////////////
// cColumnSort()
// -------------
// Sort the table by specified column

void IngredientView::columnSort(int column)
{
    // indicator should show opposite of sort
    Qt::SortOrder sortorder = Qt::DescendingOrder;
    Qt::SortOrder indicatororder = Qt::AscendingOrder;

    if (horizontalHeader()->sortIndicatorSection() == column) {
        if (horizontalHeader()->sortIndicatorOrder() == Qt::AscendingOrder) {
            sortorder = Qt::DescendingOrder;
            indicatororder = Qt::AscendingOrder;
        } else {
            sortorder = Qt::AscendingOrder;
            indicatororder = Qt::DescendingOrder;
        }
    }

    sortByColumn(column, sortorder);
    horizontalHeader()->setSortIndicator(column, indicatororder);
    horizontalHeader()->setSortIndicatorShown(true);
}

Generated by  Doxygen 1.6.0   Back to index