Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages   Examples  

example2.cpp

Example 2

This example shows how an application programmer can control the behaviour of the table by inheriting the QdbtTabular class. In the example three modified tables are created. The following is changed:


example2.png


The header file:
#ifndef _EXAMPLE2_H
#define _EXAMPLE2_H

#include <qwidget.h>

#include <qdbt/qdbttabular.h>

class QLabel;
class QPopupMenu;

class Example2Tabular : public QdbtTabular
{
  Q_OBJECT

  Example2Tabular( const Example2Tabular & );
  void operator=( const Example2Tabular & );
  
public:
  Example2Tabular( QWidget *parent = 0, const char *name = 0, WFlags f = 0 );
  ~Example2Tabular();
  void buildPopupMenu();
  
signals:
  void setStatus( const char * );
  
public slots:
  void setCellText( int index );
  
protected:
  void mouseMoveEvent( QMouseEvent * );
  void mousePressEvent( QMouseEvent * );
  void leaveTableEvent( QEvent *e );
  void resizeEvent( QResizeEvent * );
  
private:
  QPopupMenu *popMenu;
  int         clickedRow;
  int         clickedCol;
};

class Example2 : public QWidget
{
  Q_OBJECT
  
  Example2( const Example2 & );
  void operator=( const Example2 & );

public:
  Example2( QWidget *parent = 0, const char *name = 0, WFlags f = 0 );
  ~Example2();
  
public slots:
  void showStatus( const char * );
  
private:
  QLabel *status;
};

#endif

The source file:

#include <qapplication.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpopupmenu.h>
#include <qpushbutton.h>

#include "example2.h"

#include <qdbt/qdbttabcell.h>

Example2Tabular::Example2Tabular( QWidget *parent, const char *name,
                                  WFlags f ) :
  QdbtTabular( parent, name, f ),
  popMenu( 0 ),
  clickedRow( -1 ),
  clickedCol( -1 )
{
}

Example2Tabular::~Example2Tabular()
{
}

void Example2Tabular::mousePressEvent( QMouseEvent *e )
{
  if ( e->button() == RightButton ) // Right mouse button was clicked
  {
    // Compute the cell that was selected
    clickedRow = findRow( e->y() );
    clickedCol = findCol( e->x() );
    // If the cell is valid we show a popup menu at the location of the 
    // mouse press. 
    if ( popMenu && clickedRow != -1 && clickedCol != -1 ) 
    {
      popMenu->popup( mapToGlobal( e->pos() ), clickedRow );
      setRowSelected( clickedRow, TRUE );
    }
  }
}

void Example2Tabular::mouseMoveEvent( QMouseEvent *e )
{
  // compute the row and col of the cell the mouse is on
  int row = findRow( e->y() ); 
  int col = findCol( e->x() );
  if ( row != -1 && col != -1 ) // location is valid
    emit setStatus( cell( row, col )->text() );  // display the cell's text
  else
    emit setStatus( "none" ); 
}

void Example2Tabular::leaveTableEvent( QEvent * )
{
  // Set the status to `none' if the mouse leaves the table 
  setStatus( "none" );
}

void Example2Tabular::resizeEvent( QResizeEvent * )
{
  // Set the column to its maximum width
  // QdbtTabular has a frame, so we must
  // substract its width twice (left and right)
  setColumnWidth( 0, width()-2*frameWidth() );
}

void Example2Tabular::setCellText( int index )
{
  // Get the selected text
  QString text = popMenu->text( index );
  // Copy the cell 
  // (no check needed because we know cell() does not return null)
  QdbtTableCell c( *cell( clickedRow, clickedCol ) );
  // Set the selected text
  c.setText( text );
  // If the chosen text differs from the one original in the table we
  // select the row
  c.setBackground( index != clickedRow ? Qt::red : QColor() );
  c.setColor( index != clickedRow ? Qt::white : QColor() );
  // Change the table so it contains the new cell
  changeCell( &c, clickedRow, clickedCol );
}

void Example2Tabular::buildPopupMenu()
{
  // create a popupMenu object
  popMenu = new QPopupMenu();
  // call setCellText() whenever the user activates an item
  connect( popMenu, SIGNAL( activated( int ) ),
           SLOT( setCellText( int ) ) );
  // add an item for each cell in the table 
  int i;
  for ( i = 0; i < numRows(); i++ )
  {
    popMenu->insertItem( cell( i, 0 )->text(), i );
  }
}



Example2::Example2( QWidget *parent, const char *name, WFlags f ) :
  QWidget( parent, name, f ),
  status( 0 )
{
  // Set the caption of the window
  setCaption( "Example 2" );

  // The Layout managers
  QGridLayout *layout  = new QGridLayout( this, 2, 1, 5 );
  QGridLayout *tables  = new QGridLayout( 1, 3, 5 );
  QBoxLayout  *buttons = new QBoxLayout( QBoxLayout::LeftToRight );
  
  // create three tables
  Example2Tabular *tabular1 = new Example2Tabular( this );
  Example2Tabular *tabular2 = new Example2Tabular( this );
  Example2Tabular *tabular3 = new Example2Tabular( this );

  // set the table size (1 column 10 rows each)
  tabular1->setDimensions( 10, 1 );
  tabular2->setDimensions( 10, 1 );
  tabular3->setDimensions( 10, 1 );

  // hide the headers of the tables
  tabular1->setHeaderVisible( FALSE );
  tabular2->setHeaderVisible( FALSE );
  tabular3->setHeaderVisible( FALSE );

  int i;
  for ( i = 0; i < 10; i++ )
  {
    QString nameLeft, nameRight, nameCenter;
    nameLeft.sprintf( "left%d\n", i );
    nameCenter.sprintf( "center%d\n", i );
    nameRight.sprintf( "right%d\n", i );
    QdbtTableCell cell;

    cell.setAlignment( AlignLeft );
    cell.setText( nameLeft );
    tabular1->changeCell( &cell, i, 0 );
    tabular1->setRowSelectable( i, FALSE );  

    cell.setAlignment( AlignCenter );
    cell.setText( nameCenter );
    tabular2->changeCell( &cell, i, 0 );
    tabular2->setRowSelectable( i, FALSE );  

    cell.setAlignment( AlignRight );
    cell.setText( nameRight );
    tabular3->changeCell( &cell, i, 0 );
    tabular3->setRowSelectable( i, FALSE );  
  }

  // Create a popup menu for each table
  tabular1->buildPopupMenu();
  tabular2->buildPopupMenu();
  tabular3->buildPopupMenu();

  // Make sure all cells fit
  tabular1->fitAll();
  tabular2->fitAll();
  tabular3->fitAll();

  // Set the minimum sizes of the tables
  tabular1->setMinimumSize( tabular1->sizeHint() );
  tabular2->setMinimumSize( tabular2->sizeHint() );
  tabular3->setMinimumSize( tabular3->sizeHint() );

  // Since all cells are visible, we do not need tooltips
  tabular1->disableTooltips();
  tabular2->disableTooltips();
  tabular3->disableTooltips();
  
  // Create the control button
  QPushButton *close  = new QPushButton( "Close", this );
  status = new QLabel( "none    ", this );

  // Set their minimum sizes
  close->setMinimumSize( close->sizeHint() );
  status->setMinimumSize( status->sizeHint() );

  // Add Widgets and layouts to the layout-manager
  layout->addLayout( tables, 0, 0 );
  layout->addLayout( buttons, 1, 0 );
  layout->setColStretch( 0, 1 ); // make the table strechable
  layout->setRowStretch( 0, 1 );

  // Add Widgets to the tables layout
  tables->addWidget( tabular1, 0, 0 );
  tables->addWidget( tabular2, 0, 1 );
  tables->addWidget( tabular3, 0, 2 );

  // Add Widgets to the button layout
  buttons->addWidget( status );
  buttons->addStretch( 1 );
  buttons->addWidget( close );

  // don't forget to activate the top layout manager
  layout->activate();

  // Let the close button quit the application
  connect( close,  SIGNAL(clicked()), qApp, SLOT(quit()) );
  
  // Send the cell that is mouse is currently over to the label
  // for each of the tables.
  connect( tabular1, SIGNAL( setStatus(const char *) ),
           SLOT( showStatus(const char *) ) );

  connect( tabular2, SIGNAL( setStatus(const char *) ),
           SLOT( showStatus(const char *) ) );

  connect( tabular3, SIGNAL( setStatus(const char *) ),
           SLOT( showStatus(const char *) ) );

  // Resize the widget to its minimal size
  resize( layout->mainWidget()->sizeHint() );
}

Example2::~Example2()
{
}

void Example2::showStatus( const char *text )
{
  if ( strcmp( status->text(), text ) )
    status->setText( text );
}

int main( int argc, char **argv )
{
  QApplication app( argc, argv );
  Example2 example;
  app.setMainWidget( &example );
  example.show();
  return app.exec();  
}

Generated on Mon Apr 22 12:21:53 2002 for QdbtTabular by doxygen1.2.15-20020421