ICS

QicsTable Documentation Set

frozen.cpp

/****************************************************************************
** $RCSfile: frozen_cpp.html,v $ $Date: 2008/06/23 16:31:52 $
**
** Copyright ( C ) 2002-2006 Integrated Computer Solutions, Inc.
** All rights reserved.
**
** This file is part of an example program for QicsTable.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "frozen.h"
#include <QVector>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMenu>


FrozenUI::FrozenUI(QicsDataModel *dm) :
    QObject()
{
    myMainWindow = new QMainWindow();

    myTable = new QicsTable(dm, myMainWindow);

    int i;

    // Set some labels on the row and column headers

    for (i = 0; i < dm->numRows(); i++)
    myTable->rowHeaderRef().cellRef(i,0).setLabel(QString("Row %1").arg(i));

    for (i = 0; i < dm->numColumns(); i++)
    myTable->columnHeaderRef().cellRef(0,i).setLabel(QString("Col %1").arg(i));

    myMainWindow->setCentralWidget(myTable);

    // Dialogs for use in the future

    myFrozenDialog = new QicsFrozenConfigDialog(myMainWindow);

    myMoveDialog = new MoveDialog(myMainWindow);

    // Menu system

    QMenuBar *menu = myMainWindow->menuBar();

    QMenu *file = new QMenu("&File",myMainWindow);
    file->addAction("E&xit", this, SLOT(destroy()));
    menu->addMenu(file);

    QMenu *view = new QMenu("&View",myMainWindow);
    view->addAction("F&reeze...", this, SLOT(doFrozenConfig()));
    view->addAction("&Move...", this, SLOT(doMoveConfig()));
    menu->addMenu(view);

    myMainWindow->show();
}

FrozenUI::~FrozenUI()
{
    delete myMainWindow;
}

void
FrozenUI::destroy(void)
{
    qApp->quit();
}

void
FrozenUI::doFrozenConfig(void)
{
    // Pop up the frozen dialog, wait until the user dismisses it

    myFrozenDialog->exec();

    // Now do what the user requested

    if (myFrozenDialog->freezeTopRows())
    myTable->freezeTopRows(myFrozenDialog->numTopFrozenRows());
    else
        myTable->unfreezeTopRows();

    if (myFrozenDialog->freezeBottomRows())
    myTable->freezeBottomRows(myFrozenDialog->numBottomFrozenRows());
    else
        myTable->unfreezeBottomRows();

    if (myFrozenDialog->freezeLeftColumns())
    myTable->freezeLeftColumns(myFrozenDialog->numLeftFrozenCols());
    else
        myTable->unfreezeLeftColumns();

    if (myFrozenDialog->freezeRightColumns())
    myTable->freezeRightColumns(myFrozenDialog->numRightFrozenCols());
    else
        myTable->unfreezeRightColumns();
}

void
FrozenUI::doMoveConfig(void)
{
    // Pop up the move dialog, wait until the user dismisses it

    myMoveDialog->exec();

    // Now do what the user requested

    if (myMoveDialog->moveToTop() || myMoveDialog->moveToBottom())
    {
    // We are moving rows

    QVector<int> rows_to_move;

    QicsSelectionList *slist = myTable->selectionList();
    QicsSelectionList::iterator iter;
    for (iter = slist->begin(); iter != slist->end(); ++iter)
    {
        // For each selection in the selection list, check to
        // ensure that the selection is an entire row (or rows).
        // If it is, add the row indices to the rows_to_move list.

        const QicsSelection &sel = *iter;

        if ((sel.leftColumn() == 0) &&
        (sel.rightColumn() == Qics::QicsLAST_COLUMN))
        {
        int nrows = rows_to_move.size();

        rows_to_move.resize(nrows + (sel.bottomRow() - sel.topRow() + 1));

        for (int i = sel.topRow(); i <= sel.bottomRow(); ++i)
        {
            rows_to_move[nrows++] = i;
        }
        }
    }

    // This ensures that the rows are moved in the correct order
    qSort(rows_to_move.begin(),rows_to_move.end());

    // Now we can move the rows to the top or the bottom
    myTable->moveRows((myMoveDialog->moveToTop() ? 0 : myTable->bottomRow() + 1),
              rows_to_move);

    delete slist;
    }

    if (myMoveDialog->moveToLeft() || myMoveDialog->moveToRight())
    {
    // We are moving columns

    QVector<int> cols_to_move;

    QicsSelectionList *slist = myTable->selectionList();
    QicsSelectionList::iterator iter;
    for (iter = slist->begin(); iter != slist->end(); ++iter)
    {
        // For each selection in the selection list, check to
        // ensure that the selection is an entire column (or columns).
        // If it is, add the column indices to the cols_to_move list.

        const QicsSelection &sel = *iter;

        if ((sel.topRow() == 0) &&
        (sel.bottomRow() == Qics::QicsLAST_ROW))
        {
        int ncols = cols_to_move.size();

        cols_to_move.resize(ncols +
                    (sel.rightColumn() - sel.leftColumn() + 1));

        for (int i = sel.leftColumn(); i <= sel.rightColumn(); ++i)
        {
            cols_to_move[ncols++] = i;
        }
        }
    }

    // This ensures that the columns are moved in the correct order
    qSort(cols_to_move.begin(),cols_to_move.end());
    // Now we can move the columns to the left or the right
    myTable->moveColumns((myMoveDialog->moveToLeft() ? 0 :
                  myTable->rightColumn() + 1),
                 cols_to_move);

    delete slist;
    }
}


MoveDialog::MoveDialog(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout *box = new QVBoxLayout(this);

    QButtonGroup *bg = new QButtonGroup(/*1, Qt::Horizontal, */box);

    _mTop = new QRadioButton();
    _mTop->setText("Move Rows to Top");

    _mBottom = new QRadioButton();
    _mBottom->setText("Move Rows to Bottom");

    _mLeft = new QRadioButton();
    _mLeft->setText("Move Columns to Left");

    _mRight = new QRadioButton();
    _mRight->setText("Move Columns to Right");

    bg->addButton(_mTop);
    bg->addButton(_mBottom);
    bg->addButton(_mLeft);
    bg->addButton(_mRight);

    box->addWidget(_mTop);
    box->addWidget(_mBottom);
    box->addWidget(_mRight);
    box->addWidget(_mLeft);

    QHBoxLayout *row = new QHBoxLayout();

    QPushButton *ok = new QPushButton("OkButton",this);
    ok->setText("OK");
    row->addWidget(ok);
    connect(ok, SIGNAL(clicked()),
        this, SLOT(accept()) );

    QPushButton *cancel = new QPushButton("CancelButton",this);
    cancel->setText("Cancel");
    row->addWidget(cancel);
    connect(cancel, SIGNAL(clicked()),
        this, SLOT(reject()) );
    box->addLayout(row);
    setLayout(box);
}


void MoveDialog::exec()
{
    _moveToTop = false;
    _moveToBottom = false;
    _moveToLeft = false;
    _moveToRight = false;

    if (QDialog::exec() == QDialog::Accepted)
    {
    if (_mTop->isChecked())
        _moveToTop = true;

    if (_mBottom->isChecked())
        _moveToBottom = true;

    if (_mLeft->isChecked())
        _moveToLeft = true;

    if (_mRight->isChecked())
        _moveToRight = true;
    }

}


FrozenApp::FrozenApp(int &argc, char **argv) :
    QApplication(argc, argv)
{
    myModel = createModel(50, 50);
    myUI = createUI(myModel);

//    setMainWidget(myUI->base());
}

FrozenApp::~FrozenApp()
{
    delete myUI;
    delete myModel;
}

QicsDataModel *
FrozenApp::createModel(int nrows, int ncols)
{
    QicsDataModel *dm = new QicsDataModelDefault(nrows, ncols);

    for (int i = 0; i < nrows; ++i)
    {
    for (int j = 0; j < ncols; ++j)
    {
        dm->setItem(i, j, (i+j));
    }
    }

    return dm;
}

FrozenUI *
FrozenApp::createUI(QicsDataModel *dm)
{
    return (new FrozenUI(dm));
}


int main(int argc, char *argv[])
{
    FrozenApp app(argc,argv);

    return app.exec();
}

All trademarks and copyrights on this page are properties of their respective owners.
The rest is copyright 1999-2006 Integrated Computer Solutions, Inc.