Next: Creación de la base Up: Código fuente programas en Previous: Código fuente programas en Contents

##
Código fuente programa qtscribble.cpp

  #include <qapplication.h>

  #include <qdialog.h>

  #include <qfiledialog.h>

  #include <qmenubar.h>

  #include <qmessagebox.h>

  #include <qpainter.h>

  #include <qpixmap.h>

  #include <qpopupmenu.h>

  #include <qscrollview.h>

  #include <qwidget.h>

  #include <qcursor.h>

  enum MenuIDs

  {

  COLOR_MENU_ID_BLACK,

  COLOR_MENU_ID_RED,

  COLOR_MENU_ID_BLUE,

  COLOR_MENU_ID_GREEN,

  COLOR_MENU_ID_YELLOW

  };

  /**

  *A class that lets the user draw with the mouse. The

  *windows knows how to redraw itlself.

  */

  class ScribbleArea : public QWidget

  {

  Q_OBJECT // necessary beacuse ScribbleArea contains slots

  public:

  ScribbleArea();

  ScribbleArea();

  public slots:

  void setColor( QColor );

  void slotLoad( const QString& );

  void slotSave( const QString& );

  protected:

  virtual void mousePressEvent( QMouseEvent* );

  virtual void mouseMoveEvent( QMouseEvent* );

  virtual void paintEvent( QPaintEvent* );

  virtual void resizeEvent( QResizeEvent* );

  private slots:

  void slotClearArea();

  private:

  QPoint _last;

  QColor _currentcolor;

  QPixmap _buffer;

  QPopupMenu* _popupmenu;

  };

  class ScribbleWindow : public QWidget

  {

  Q_OBJECT

  public:

  ScribbleWindow();

  ScribbleWindow();

  private slots:

  void slotAbout();

  void slotAboutQt();

  void slotColorMenu( int );

  void slotLoad();

  void slotSave();

  signals:

  void colorChanged( QColor );

  void load( const QString& );

  void save( const QString& );

  protected:

  virtual void resizeEvent( QResizeEvent* );

  private:

  QMenuBar* _menubar;

  QPopupMenu* _filemenu;

  QPopupMenu* _colormenu;

  QPopupMenu* _helpmenu;

  QScrollView* _scrollview;

  ScribbleArea* _scribblearea;

  };

  #include "qtscribble.moc"

  /**

  * The constructor. Initializes the member variables, and creates a

  * pop-up menu that will pop up when the right mouse button is

  * clicked

  */

  ScribbleArea::ScribbleArea()

  {

  //initialize member variables

  _currentcolor = black;

  // dont blank the window before repainting

  setBackgroundMode( NoBackground );

  // create a popup menu

  _popupmenu = new QPopupMenu();

  _popupmenu->insertItem( "&Clear", this, SLOT( slotClearArea() ) );

  }

  /**

  * The destructor.Deletes the pop-upmenu.

  */

  ScribbleArea::ScribbleArea()

  {

  delete _popupmenu;

  }

  /**

  * Thsi slot sets the current color for the drawing area. It will be

  * conected with the color Changed( QColor ) signal from the ScribbleWindow.

  *

  */

  void ScribbleArea::setColor( QColor new_color )

  {

  _currentcolor = new_color;

  }

  /**

  * This slot clears the drawing area by filling the off-screen buffer with

  * white , and then ncopying it over to the window.

  */

  void ScribbleArea::slotClearArea()

  {

  // fill the off-screen buffer with plain white

  _buffer.fill( white );

  }

  /**

  * This method does the actual loading. It relies on QPixmap ( and the

  * underlying I/O machinery ) to determine the filetype.

  */

  void ScribbleArea::slotLoad( const QString& filename )

  {

  if( !_buffer.load( filename ) )

  QMessageBox::warning( 0,"Load error", "Could not load file" );

  repaint(); // refresh the window

  }

  /**

  * this mehtod does the actual saving. We hard-code the file type as

  * BMP. Unix users might want to replace this file type with XPM.

  */

  void ScribbleArea::slotSave( const QString& filename )

  {

  if( !_buffer.save( filename, "XPM" ) )

  QMessageBox::warning( 0, "Save error", "Could not save file " );

  }

  /**

  * This virtual method is called when the pointer is on the window

  * and the user presses the mouse button. When the right mouse button

  * is pressed, it pops up a previously constructed pop-up menu. Otherwise,

  * it just records the position of the mouse at the time of the click.

  */

  void ScribbleArea::mousePressEvent( QMouseEvent* event )

  {

  if( event->button() == RightButton )

  _popupmenu->exec( QCursor::pos() );

  else

  {

  _last = event->pos(); // retrieve the coordinates from the event

  }

  }

  /**

  * This virtual method is called whenever the user moves the mouse

  * while the mouse button is pressed. If we had called

  * setMouseTracking( true ) before, this method would also be called

  * when the mouse was moved without any button pressed. We know that

  * we haven't, and thus don't have to check whether any buttons are

  * pressed.

  */

  void ScribbleArea::mouseMoveEvent( QMouseEvent* event )

  {

  // create a QPainter object for drawing onto the window

  QPainter windowpainter;

  // and other QPainter object for drawing into off-screen pixmap

  QPainter bufferpainter;

  // start painting

  windowpainter.begin( this ); //this painter paints into the window

  bufferpainter.begin( &_buffer ); // and this one in the buffer

  // set a standard pen with the currently selected color

  windowpainter.setPen( _currentcolor );

  bufferpainter.setPen( _currentcolor );

  // draw a line in both the window and the buffer

  windowpainter.drawLine( _last, event->pos() );

  bufferpainter.drawLine( _last, event->pos() );

  // done with painting

  windowpainter.end();

  bufferpainter.end();

  // remember the current mouse position

  _last = event->pos();

  }

  /**

  * This virtual method is called whenever the widget needs

  * painting. Such as when it has been obscured and then revealed again.

  */

  void ScribbleArea::paintEvent( QPaintEvent* event )

  {

  // copy the image from the buffer pixmap to the window

  bitBlt( this, 0, 0, &_buffer );

  }

  /**

  * This virtual method is called whenever the window is resized. We

  * use it to make sure that the off-screen buffer is always the same

  * size as the window.

  * To retain the original drawing, it is first copied

  * to a temporary buffer. After the main buffer has been resized and

  * filled with white, the image is copied from the temporary buffer to

  * the main buffer.

  */

  void ScribbleArea::resizeEvent( QResizeEvent* event )

  {

  QPixmap save( _buffer );

  _buffer.resize( event->size() );

  _buffer.fill( white );

  bitBlt( &_buffer, 0, 0, &save );

  }

  ScribbleWindow::ScribbleWindow()

  {

  /* The next few lines build up the menu bar. We first create the menus

  * one by one, then add them to the menu bar. */

  _filemenu = new QPopupMenu; // create the file menu

  _filemenu->insertItem( "&Load", this, SLOT( slotLoad() ) );

  _filemenu->insertItem( "&Save", this, SLOT( slotSave() ) );

  _filemenu->insertSeparator();

  _filemenu->insertItem( "&Quit", qApp, SLOT( quit() ) );

  _colormenu = new QPopupMenu; // create de color menu

  _colormenu->insertItem("B&lack", COLOR_MENU_ID_BLACK );

  _colormenu->insertItem("&Red", COLOR_MENU_ID_RED );

  _colormenu->insertItem("&Blue", COLOR_MENU_ID_BLUE );

  _colormenu->insertItem("&Green", COLOR_MENU_ID_GREEN );

  _colormenu->insertItem("&Yellow", COLOR_MENU_ID_YELLOW );

  QObject::connect( _colormenu, SIGNAL( activated( int ) ),

  this, SLOT( slotColorMenu( int ) ) );

  _helpmenu = new QPopupMenu; // create the help menu

  _helpmenu->insertItem("&About QtScribble", this, SLOT( slotAbout() ) );

  _helpmenu->insertItem( "About &Qt", this, SLOT( slotAboutQt() ) );

  _menubar = new QMenuBar( this ); // create a menu bar

  _menubar->insertItem( "&File", _filemenu );

  _menubar->insertItem( "&Color", _colormenu );

  _menubar->insertSeparator();

  _menubar->insertItem( "&Help", _helpmenu );

  /** We create a QScrollView and ScribbleArea. The ScribbleArea will

  * be managed by scroll view */

  _scrollview = new QScrollView( this );

  _scrollview->s<?php

  $num_to_guess = 42;

  $message = "";

  if (!isset($_POST[guess])) {

  $message = "Bienvenido a la maquina de adivinar!";

  } elseif ($_POST[guess] > $num_to_guess) {

  $message = "$_POST[guess] es my grande! Intente un numero menor";

  } elseif ($_POST[guess] < $num_to_guess) {

  $message = "$_POST[guess] es muy pequenio! Intente un numero mas grande";

  } else { // must be equivalent

  $message = "Bien hecho!";

  }

  ?>

  <html>

  <head>

  <title>Un script de PHP para adivinar un numero</title>

  </head>

  <body>

  <h1>

  <?php print $message ?>

  </h1>

  <form action="<?php print $_SERVER[PHP_SELF] ?>" method="POST">

  Escriba su numero aqui: <input type="text" name="guess">

  </form>

  </body>

  </html>etGeometry( 0, _menubar->height(), width(), height() - _menubar->height() );

  _scribblearea = new ScribbleArea();

  _scribblearea->setGeometry( 0, 0, 1000, 1000 );

  _scrollview->addChild( _scribblearea );

  QObject::connect( this, SIGNAL( colorChanged( QColor ) ), _scribblearea, SLOT( setColor( QColor ) ) );

  QObject::connect( this, SIGNAL( save( const QString& ) ), _scribblearea, SLOT( slotSave( const QString& ) ) );

  QObject::connect( this, SIGNAL( load( const QString& ) ), _scribblearea, SLOT( slotLoad( const QString& ) ) );

  }

  ScribbleWindow::ScribbleWindow()

  {

  }

  void ScribbleWindow::resizeEvent( QResizeEvent* event )

  {

  /* When the whole window is resized, we have to rearrage the geometry

  * in the ScribbleWindow as well. Note that the ScribbleArea does not need

  * to be changed */

  _scrollview->setGeometry( 0, _menubar->height(), width(), height() - _menubar->height() );

  }

  void ScribbleWindow::slotAbout()

  {

  QMessageBox::information( this, "About QtScribble 4","This is the QtScribble 4 application\n (C) 1998-2001 Matthias Kalle Dalheimer\n");

  }

  void ScribbleWindow::slotAboutQt()

  {

  QMessageBox::aboutQt( this, "About Qt" );

  }

  void ScribbleWindow::slotColorMenu( int item )

  {

  switch( item )

  {

  case COLOR_MENU_ID_YELLOW:

  emit colorChanged( yellow );

  break;

  case COLOR_MENU_ID_RED:

  emit colorChanged( darkRed );

  break;

  case COLOR_MENU_ID_BLUE:

  emit colorChanged( darkBlue );

  break;

  case COLOR_MENU_ID_GREEN:

  emit colorChanged( darkGreen );

  break;

  case COLOR_MENU_ID_BLACK:

  emit colorChanged( black );

  break;

  }

  }

  /**

  * This is the slot for the menu item File/Load. It opens a

  * Qfiledialog to ask the user for a filename, then emits a save()

  * signal with the filename as a parameter

  */

  void ScribbleWindow::slotLoad()

  {

  /** Open a file dialog for loading. The default directory is the

  * current directory, the filter *.bmp.

  */

  QString filename = "test.gif";

  //QString filename = QFileDialog::getOpenFileName(".","Images(*.gif)", this );

  /** This is the save equivalent to slotLoad(). Again, we just ask for a

  *filename and emit a signal.

  */

  void ScribbleWindow::slotSave()

  {

  /*Open a file dialog for saving. The default directory is the

  * current directory, the filter *.bmp.

  */

  QString filename = "test.gif";

  //QString filename = QFileDialog::getSaveFileName(".","Images(*.gif)", this);

  if( !filename.isEmpty() )

  emit save( filename );

  }

  int main( int argc, char* argv[] )

  {

  QApplication myapp( argc, argv );

  ScribbleWindow* mywidget = new ScribbleWindow();

  mywidget->setGeometry( 50, 50, 400, 400 );

  myapp.setMainWidget( mywidget );

  mywidget->show();

  return myapp.exec();

  }. Ejecutando el siguiente comando se tendrï¿12todo el directorio incluï¿12o en el paquete

Next: Creación de la base Up: Código fuente programas en Previous: Código fuente programas en Contents