ICS

QicsTable Documentation Set

frozen.cpp

/****************************************************************************
** $RCSfile: frozen_cpp.html,v $ $Date: 2008/06/23 16:31:50 $
**
** 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"


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("frozenConfigDialolg",
                        myMainWindow);

    myMoveDialog = new MoveDialog("moveDialog", myMainWindow);

    // Menu system

    QMenuBar *menu = myMainWindow->menuBar();
    
    QPopupMenu *file = new QPopupMenu(myMainWindow);
    file->insertItem("E&xit", this, SLOT(destroy()));
    menu->insertItem("&File", file);

    QPopupMenu *view = new QPopupMenu(myMainWindow);
    view->insertItem("F&reeze...", this, SLOT(doFrozenConfig()));
    view->insertItem("&Move...", this, SLOT(doMoveConfig()));
    menu->insertItem("&View", 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());

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

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

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

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

    QMemArray<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
    rows_to_move.sort();

    // 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

    QMemArray<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
    cols_to_move.sort();

    // 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(const char *name, QWidget *parent) :
    QDialog(parent, name)
{
    QVBoxLayout *box = new QVBoxLayout(this, 2);

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

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

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

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

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

    box->addWidget(bg);

    QHBoxLayout *row = new QHBoxLayout(box);

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

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


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

    if (QDialog::exec() == QDialog::Accepted)
    {
    if (_mTop->isOn())
        _moveToTop = true;
    
    if (_mBottom->isOn())
        _moveToBottom = true;
    
    if (_mLeft->isOn())
        _moveToLeft = true;

    if (_mRight->isOn())
        _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.