ICS

QicsTable Documentation Set

consultant.cpp

/****************************************************************************
** $RCSfile: consult_cpp.html,v $ $Date: 2008/07/16 14:58:59 $
**
** 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 "consultant.h"

#include <QFileDialog>
#include <QTextStream>
#include <QLabel>
#include <QMenu>

#include <QicsDateTimeCellDisplay.h>

#define NUM_COLUMNS 7

/*
 * SSNValidator Methods
 */

QValidator::State SSNValidator::validate( QString& text, int& pos ) const
{
    /* An empty string could be made acceptable */
    if( text == QString::null )
    {
        return Intermediate;
    }

    /* Protect against spurious calls to validate() */
    if( pos > text.length() )
    {
        return Intermediate;
    }

   /* Anything but decimal digits and dashes is invalid. We only need
    * to check the character at the cursor positions. This speeds
    * things up massively.
    */

    if( ( pos-1 >= 0 ) && !text[pos-1].isDigit() && ( text[pos-1] != '-' ) )
    {
        return Invalid;
    }

    /* If the characters entered so far are valid, but the string
    * contains less than ten digits, it could be made acceptable, but
    * is not yet.
    */
    int numDigits = text.length();
    int numDashes = 0;

    for (int i=0; i < text.length(); i++)
    {
        if(text.at(i) == QChar('-'))
        {
                numDigits--;
                numDashes++;
        }
    }

    if( numDigits < 9 )
    {
        return Intermediate;
    }

    /* More than nine digits is always invalid */
    if( numDigits > 9 )
    {
        return Invalid;
    }

    if (numDashes != 0)
    {
        return Intermediate;
    }

    /* numDashes = 0 and numDigits = 9 */
    return Acceptable;
}

void
SSNValidator::fixup( QString& text ) const
{
    QString inString = text;
    int inLength = inString.length();

    for ( int i=inLength; i > 0; i--)
    {
        if( inString[i] == '-' )
        {
            text.remove(i,1);
            qDebug ("Removing dash. \n");
        }
    }
}


/*
 * The main Consult class
 */

Consultant::Consultant(QWidget *parent) :
    QMainWindow(parent)
{
    // create the data model
    dataModel = new QicsDataModelDefault(0,NUM_COLUMNS);

    // create the table, using the data model we created above
    table = new QicsTable(dataModel, this);


    setCentralWidget(table);

    // customize the column header to make things look pretty
    table->columnHeaderRef().setAlignment(Qt::AlignCenter);

    table->columnHeaderRef().cellRef(0,0).setLabel(QString("First Name"));
    table->columnHeaderRef().cellRef(0,1).setLabel(QString("Last Name"));
    table->columnHeaderRef().cellRef(0,2).setLabel(QString("S.S. Number"));
    table->columnHeaderRef().cellRef(0,3).setLabel(QString("Date of Birth"));
    table->columnHeaderRef().cellRef(0,4).setLabel(QString("Office"));
    table->columnHeaderRef().cellRef(0,5).setLabel(QString("Sec. Clearance"));
    table->columnHeaderRef().cellRef(0,6).setLabel(QString("Billable Rate"));

    //Add a validator to the SSN to insure a consistant display
    //behaviour and error checking.
    SSNValidator* ssnValidator = new SSNValidator();
    table->columnRef(2).setValidator(ssnValidator);

    //Format the combo box for the office column
    QicsComboCellDisplay *officeComboDisplay = new QicsComboCellDisplay();
    officeComboDisplay->addItem(QString("Boston"));
    officeComboDisplay->addItem(QString("Cambridge"));
    officeComboDisplay->addItem(QString("Helsinki"));
    officeComboDisplay->setAddValueToList(false);
    table->columnRef(4).setDisplayer(officeComboDisplay);

    // Security clearance check box
    QicsCheckCellDisplay *columnHeaderCheckDisp = new QicsCheckCellDisplay();
    table->columnRef(5).setDisplayer(columnHeaderCheckDisp);

    // Creating date time displayer
    QicsDateTimeCellDisplay* dt = new QicsDateTimeCellDisplay;
    dt->setDisplayFormat("dd/MM/yyyy");

    table->columnRef(3).setDisplayer(dt);

    //Format the billable rate column with a dollar sign, decimal point,
    //and the appropriate cents digits.
    QicsDataItemSprintfFormatter *brFormatter = new QicsDataItemSprintfFormatter();
    brFormatter->addFormatString(QicsDataItem_Float, "$%.2f");
    brFormatter->addFormatString(QicsDataItem_Int, "$%d.00");
    table->columnRef(6).setFormatter(brFormatter);
    table->columnRef(6).setValidator(new QDoubleValidator(table));

    // Add a title widget to the top of the table
    QLabel *label = new QLabel(QString("Consultants"), table);
    label->setAlignment(Qt::AlignCenter);
    table->setTopTitleWidget(label);

    //Set column widths
    for (int i = 0; i < NUM_COLUMNS; i++)
    {
        table->columnRef(i).setWidthInChars(7);
    }

    table->setVisibleColumns(NUM_COLUMNS);

    //Menu Bar
    QMenu *filemenu = new QMenu("File",this);

    QAction *save = filemenu->addAction( "Save..." );
    connect(save,SIGNAL(triggered()),this,SLOT ( fileSave()));

    QAction *open = filemenu->addAction( "Open..." );
    connect(open,SIGNAL(triggered()),this,SLOT ( fileOpen()));

    QAction *quit = filemenu->addAction( "Quit" );
    connect(quit,SIGNAL(triggered()),qApp,SLOT(quit()));

    QMenu *toolsmenu = new QMenu("Tools",this);

    QAction *addConsultant = toolsmenu->addAction( "Add Consultant");
    connect(addConsultant,SIGNAL(triggered()),this,SLOT(addRow()));

    QAction *deleteConsultant = toolsmenu->addAction( "Delete Consultant");
    connect(deleteConsultant,SIGNAL(triggered()),this,SLOT(deleteRow()));

    QAction *sortAscending = toolsmenu->addAction("Sort Ascending");
    connect(sortAscending,SIGNAL(triggered()),this,SLOT(sortAscending()));

    QAction *sortDescending = toolsmenu->addAction("Sort Descending");
    connect(sortDescending,SIGNAL(triggered()),this,SLOT(sortDescending()));

    QMenuBar *menubar = menuBar();
    menubar->addMenu(filemenu);
    menubar->addMenu(toolsmenu);

    //Connection for autosort
    QObject::connect( &(table->columnHeaderRef()),
              SIGNAL( doubleClicked(int , int , int , const QPoint&) ),
              this,
              SLOT( autoSort(int,  int , int , const QPoint&) ) );

    //Reset autoSort sanity vars
    _autoSortCol = -1;
    _autoSortOrder = Qics::Descending;
}

void Consultant::fileSave()
{
    QString outFileName = QFileDialog::getSaveFileName(this,"Choose a file", "./",
                              "Serialized data files (*.dat)");

    QFile file(outFileName);

    if(!file.open(QIODevice::WriteOnly))
    {
        qWarning("error opening file for writing...");
        return;
    }
    QDataStream ds(&file);

    int nrows = table->dataModel()->numRows();
    int ncols = table->dataModel()->numColumns();

    ds<< nrows << ncols;

    for(int i = 0;i < nrows;i++)
    {
         for(int j = 0;j < ncols;j++)
         {
                 const QicsDataItem* itm = table->dataModel()->item(i,j);
                 if(itm)
                     itm->encode(ds);
                 else
                 {
                     // if item is NULL creating empty string and serializing it!
                     QicsDataString str;
                     str.encode(ds);
                 }
         }
    }
    file.close();
}

void Consultant::fileOpen()
{

    QString inFileName = QFileDialog::getOpenFileName(this,"Choose a filename", "./",
                              "Serialized data files (*.dat)");

    if (inFileName.isEmpty())
        return;

    QFile file(inFileName);
    if(!file.open(QIODevice::ReadOnly))
    {
       qWarning("error opening file for reading...");
       return;
    }

    QDataStream ds(&file);

    int rows;
    int columns;

    ds>>rows;
    ds>>columns;

    table->setIgnoreModelSizeChanges(true);
    table->setRepaintBehavior(Qics::RepaintOff);

    table->dataModel()->clearModel();
    table->dataModel()->addRows(rows);
    table->dataModel()->addColumns(columns);

    table->clearSelectionList();
    for(int i = 0;i<rows;i++)
    {
        for(int j = 0;j<columns;j++)
        {
            QicsDataItem *it = QicsDataItem::decode(ds);
            if(it)
               table->dataModel()->setItem(i,j, *it);
        }
    }
    table->setIgnoreModelSizeChanges(false);
    table->setRepaintBehavior(Qics::RepaintOn);

    file.close();
}

void
Consultant::addRow()
{
    table->addRows(1);
}

void
Consultant::deleteRow()
{
    table->deleteRow(table->currentCell()->rowIndex());
}

void
Consultant::sortAscending()
{
    table->sortRows(table->currentCell()->columnIndex(), Qics::Ascending);
}

void
Consultant::sortDescending()
{
    table->sortRows(table->currentCell()->columnIndex(), Qics::Descending);
}


void
Consultant::autoSort(int , int col, int , const QPoint &)
{
    if (_autoSortCol == col)
    {
        if (_autoSortOrder == Qics::Ascending)
            _autoSortOrder = Qics::Descending;
        else
            _autoSortOrder = Qics::Ascending;
    }
    else
    {
        _autoSortCol = col;
        _autoSortOrder = Qics::Ascending;
    }

    table->sortRows(col, _autoSortOrder);
}

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