⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formwindow.cpp

📁 Linux下的基于X11的图形开发环境。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/************************************************************************ Copyright (C) 2000 Trolltech AS.  All rights reserved.**** This file is part of Qt Designer.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "formwindow.h"#include "defs.h"#include "mainwindow.h"#include "widgetfactory.h"#include "sizehandle.h"#include "metadatabase.h"#include "resource.h"#include "layout.h"#include "connectiondialog.h"#include <widgetdatabase.h>#include "pixmapchooser.h"#include "orderindicator.h"#include "hierarchyview.h"#include "designerappiface.h"#define NO_STATIC_COLORS#include "globaldefs.h"#include <stdlib.h>#include <qevent.h>#include <qpainter.h>#include <qpen.h>#include <qlabel.h>#include <qobjectlist.h>#include <qtimer.h>#include <qapplication.h>#include <qlayout.h>#include <qspinbox.h>#include <qstatusbar.h>#include <qapplication.h>#include <qpalette.h>#include <qmessagebox.h>#include <qpopupmenu.h>#include <qsizegrip.h>#include <qpushbutton.h>#include <qwhatsthis.h>#include <qmetaobject.h>#include <qtooltip.h>#include <qfeatures.h>#include <qaccel.h>#include <qpixmapcache.h>#include <qbitmap.h>// FormWindow should be able to work to some limited degree// (existance, loading) without a MainWindow. Functions which require// a MainWindow in theory should never be called if no MainWindow// exists. These macros are used to let us know if that happens anyway// and to ensure that we don't crash#define CHECK_MAINWINDOW Q_ASSERT( mainWindow() ); if ( !mainWindow() ) return#define CHECK_MAINWINDOW_VALUE( v ) Q_ASSERT( mainWindow() ); if ( !mainWindow() ) return vstatic void setCursorToAll( const QCursor &c, QWidget *start ){    start->setCursor( c );    QObjectList *l = (QObjectList*)start->children();    if ( l ) {	for ( QObject *o = l->first(); o; o = l->next() ) {	    if ( o->isWidgetType() && !o->inherits( "SizeHandle" ) )		setCursorToAll( c, ( (QWidget*)o ) );	}    }}static void restoreCursors( QWidget *start, FormWindow *fw ){    if ( fw->widgets()->find( start ) )	start->setCursor( MetaDataBase::cursor( start ) );    else	start->setCursor( Qt::ArrowCursor );    QObjectList *l = (QObjectList*)start->children();    if ( l ) {	for ( QObject *o = l->first(); o; o = l->next() ) {	    if ( o->isWidgetType() && !o->inherits( "SizeHandle" ) )		restoreCursors( ( (QWidget*)o ), fw );	}    }}#if defined(Q_WS_WIN32) // #### needed for the workaround for repaint problem on windows#include <qt_windows.h>static void flickerfree_update( QWidget *w ){    InvalidateRect( w->winId(), 0, FALSE );}#endif/*!  \class FormWindow formwindow.h  \brief Editor window for a form  The FormWindow is the widget which is used as editor for forms. It  handles inserting, deleting, moving, resizing, etc. of widgets.  Normally multiple formwindows are used at the same time in the  Designer. So each formwindow has its own undo/redo buffer, etc.  Also the formwindow has some signals to inform e.g. about selection  changes which is interesting for the PropertyEditor.  For handling the events of the child widgets (moving, etc.) the  handleMousePress(), etc. functions are called from the application  event filter which is implemented in MainWindow::eventFilter().*/FormWindow::FormWindow( FormFile *f, MainWindow *mw, QWidget *parent, const char *name )    : QWidget( parent, name, WDestructiveClose ), mainwindow( mw ),      commands( 100 ), pixInline( TRUE ), pixProject( FALSE ){    ff = f;    init();    initSlots();}FormWindow::FormWindow( FormFile *f, QWidget *parent, const char *name )    : QWidget( parent, name, WDestructiveClose ), mainwindow( 0 ),      commands( 100 ), pixInline( TRUE ){    ff = f;    init();}void FormWindow::init(){    fake = qstrcmp( name(), "qt_fakewindow" ) == 0;    MetaDataBase::addEntry( this );    ff->setFormWindow( this );    iface = 0;    proj = 0;    propertyWidget = 0;    toolFixed = FALSE;    checkedSelectionsForMove = FALSE;    mContainer = 0;    startWidget = endWidget = 0;    currTool = POINTER_TOOL;    unclippedPainter = 0;    widgetPressed = FALSE;    drawRubber = FALSE;    setFocusPolicy( ClickFocus );    sizePreviewLabel = 0;    checkSelectionsTimer = new QTimer( this, "checkSelectionsTimer" );    connect( checkSelectionsTimer, SIGNAL( timeout() ),	     this, SLOT( invalidCheckedSelections() ) );    updatePropertiesTimer = new QTimer( this );    connect( updatePropertiesTimer, SIGNAL( timeout() ),	     this, SLOT( updatePropertiesTimerDone() ) );    showPropertiesTimer = new QTimer( this );    connect( showPropertiesTimer, SIGNAL( timeout() ),	     this, SLOT( showPropertiesTimerDone() ) );    selectionChangedTimer = new QTimer( this );    connect( selectionChangedTimer, SIGNAL( timeout() ),	     this, SLOT( selectionChangedTimerDone() ) );    windowsRepaintWorkaroundTimer = new QTimer( this );    connect( windowsRepaintWorkaroundTimer, SIGNAL( timeout() ),	     this, SLOT( windowsRepaintWorkaroundTimerTimeout() ) );    insertParent = 0;    connect( &commands, SIGNAL( undoRedoChanged( bool, bool, const QString &, const QString & ) ),	     this, SIGNAL( undoRedoChanged( bool, bool, const QString &, const QString & ) ) );    propShowBlocked = FALSE;    setIcon( QPixmap::fromMimeSource( "form.png" ) );    connect( &commands, SIGNAL( modificationChanged( bool ) ),	     this, SLOT( modificationChanged( bool ) ) );    buffer = 0;    QWidget *w = WidgetFactory::create( WidgetDatabase::idFromClassName( "QFrame" ), this );    setMainContainer( w );    propertyWidget = w;    targetContainer = 0;    hadOwnPalette = FALSE;    defSpacing = BOXLAYOUT_DEFAULT_SPACING;    defMargin = BOXLAYOUT_DEFAULT_MARGIN;    hasLayoutFunc = FALSE;}void FormWindow::setMainWindow( MainWindow *w ){    mainwindow = w;    MetaDataBase::addEntry( this );    initSlots();}void FormWindow::initSlots(){    if ( isFake() )	return;    Q_ASSERT( project() || MainWindow::self );    if ( !project() && !MainWindow::self )	return;    Project *p = project() ? project() : MainWindow::self->currProject();    if ( p && p->isCpp() ) {	QString code = formFile()->code();	if ( code.isEmpty() )	    formFile()->setCode( formFile()->codeComment() );    }}FormWindow::~FormWindow(){    if ( MainWindow::self && MainWindow::self->objectHierarchy()->formWindow() == this )	MainWindow::self->objectHierarchy()->setFormWindow( 0, 0 );    MetaDataBase::clear( this );    if ( ff )	ff->setFormWindow( 0 );    delete iface;}void FormWindow::closeEvent( QCloseEvent *e ){    if ( ff->closeEvent() && mainwindow && mainwindow->unregisterClient( this ) )	e->accept();    else	e->ignore();}void FormWindow::paintGrid( QWidget *w, QPaintEvent *e ){    if ( !mainWindow() || !mainWindow()->showGrid() )	return;    QPixmap grid;    QString grid_name;    grid_name.sprintf("FormWindowGrid_%d_%d", mainWindow()->grid().x(), mainWindow()->grid().y());    if( !QPixmapCache::find( grid_name, grid ) ) {	grid = QPixmap( 350 + ( 350 % mainWindow()->grid().x() ), 350 + ( 350 % mainWindow()->grid().y() ) );	grid.fill( colorGroup().color( QColorGroup::Foreground ) );	QBitmap mask( grid.width(), grid.height() );	mask.fill( color0 );	QPainter p( &mask );	p.setPen( color1 );	for ( int y = 0; y < grid.width(); y += mainWindow()->grid().y()) {	    for ( int x = 0; x < grid.height(); x += mainWindow()->grid().x() ) {		p.drawPoint( x, y );	    }	}	grid.setMask( mask );	QPixmapCache::insert( grid_name, grid );    }    QPainter p( w );    p.setClipRegion( e->rect() );    p.drawTiledPixmap( QRect( 0, 0, width(), height() ), grid );}/*!  For operations like drawing a rubber band or drawing the rect  when inserting a new widget, a unclipped painter (which draws also  on child widgets) is needed. This method does all the initialization.*/void FormWindow::beginUnclippedPainter( bool doNot ){    endUnclippedPainter();    bool unclipped = testWFlags( WPaintUnclipped );    setWFlags( WPaintUnclipped );    unclippedPainter = new QPainter;    unclippedPainter->begin( this );    if ( !unclipped )	clearWFlags( WPaintUnclipped );    if ( doNot ) {	unclippedPainter->setPen( QPen( color0, 2 ) );	unclippedPainter->setRasterOp( NotROP );    }}/*!  Gets rid of an open unclipped painter.  \sa beginUnclippedPainter()*/void FormWindow::endUnclippedPainter(){    if ( unclippedPainter )	unclippedPainter->end();    delete unclippedPainter;    unclippedPainter = 0;}QPoint FormWindow::gridPoint( const QPoint &p ){    return QPoint( ( p.x() / grid().x() ) * grid().x(),		   ( p.y() / grid().y() ) * grid().y() );}void FormWindow::drawSizePreview( const QPoint &pos, const QString& text ){    unclippedPainter->save();    unclippedPainter->setPen( QPen( colorGroup().foreground(), 1  ));    unclippedPainter->setRasterOp( CopyROP );    if ( !sizePreviewPixmap.isNull() )	unclippedPainter->drawPixmap( sizePreviewPos, sizePreviewPixmap );    if ( text.isNull() ) {	sizePreviewPixmap = QPixmap(); // set null again	unclippedPainter->restore();	return;    }    QRect r  =  fontMetrics().boundingRect( 0, 0, 0, 0, AlignCenter, text );    r = QRect( pos + QPoint( 10, 10 ), r.size() + QSize( 5, 5 ) );    checkPreviewGeometry( r );    sizePreviewPos = r.topLeft();    sizePreviewPixmap = QPixmap::grabWindow( winId(), r.x(), r.y(), r.width(), r.height() );    unclippedPainter->setBrush( QColor( 255, 255, 128 ) );    unclippedPainter->drawRect( r );    unclippedPainter->drawText( r, AlignCenter, text );    unclippedPainter->restore();}void FormWindow::insertWidget(){    CHECK_MAINWINDOW;    if ( !insertParent )	return;    if ( currTool == POINTER_TOOL )	return;    bool useSizeHint = !oldRectValid || ( currRect.width() < 2 && currRect.height() < 2 );    Orientation orient = Horizontal;    QString n = WidgetDatabase::className( currTool );    if (  useSizeHint && ( n == "Spacer" || n == "QSlider" || n == "Line" || n == "QScrollBar" ) ) {	QPopupMenu m( mainWindow() );	m.insertItem( tr( "&Horizontal" ) );	int ver = m.insertItem( tr( "&Vertical" ) );	int r = m.exec( QCursor::pos() );	if ( r == ver )	    orient = Vertical;    }    QWidget *w = WidgetFactory::create( currTool, insertParent, 0, TRUE, &currRect, orient );    if ( !w )	return;    if ( !savePixmapInline() && currTool == WidgetDatabase::idFromClassName( "PixmapLabel" ) ) // ### what to do for pixmaps in project	( (QLabel*)w )->setPixmap( QPixmap::fromMimeSource( "image.png" ) );    int id = WidgetDatabase::idFromClassName( WidgetFactory::classNameOf(w) );    if ( WidgetDatabase::isCustomWidget( id ) ) {	QWhatsThis::add( w, tr("<b>A %1 (custom widget)</b> "			    "<p>Click <b>Edit Custom Widgets...</b> in the <b>Tools|Custom</b> "			    "menu to add and change custom widgets. You can add "			    "properties as well as signals and slots to integrate custom widgets into "			    "<i>Qt Designer</i>, and provide a pixmap which will be used to represent "			    "the widget on the form.</p>")			    .arg(WidgetDatabase::toolTip( id )) );	QToolTip::add( w, tr("A %1 (custom widget)").arg(WidgetDatabase::toolTip( id )) );    } else {	QString tt = WidgetDatabase::toolTip( id );	QString wt = WidgetDatabase::whatsThis( id );	if ( !wt.isEmpty() && !tt.isEmpty() )	    QWhatsThis::add( w, QString("<b>A %1</b><p>%2</p>").arg( tt ).arg( wt ) );    }    QString s = w->name();    unify( w, s, TRUE );    w->setName( s );    insertWidget( w );    QRect r( currRect );    if ( !oldRectValid ||	 ( currRect.width() < 2 && currRect.height() < 2 ) )	r = QRect( rectAnchor, QSize( 0, 0 ) );    QPoint p = r.topLeft();    p = mapToGlobal( p );    p = insertParent->mapFromGlobal( p );    r = QRect( p, r.size() );    if ( useSizeHint ) {	if ( n == "Spacer" ) {	    if ( orient == Vertical ) {		r.setWidth( 20 );		r.setHeight( 40 );	    } else {		r.setWidth( 40 );		r.setHeight( 20 );	    }	} else {	    r.setWidth( w->sizeHint().width() );	    r.setHeight( w->sizeHint().height() );	}    }    if ( r.width() < 2 * grid().x() )	r.setWidth( 2 * grid().x() );    if ( r.height() < 2 * grid().y() )	r.setHeight( 2 * grid().y() );    const QObjectList *l = insertParent->children();    QObjectListIt it( *l );    QWidgetList lst;    if ( WidgetDatabase::isContainer( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( w ) ) ) ) {	for ( ; it.current(); ) {	    QObject *o = it.current();	    ++it;	    if ( o->isWidgetType() &&		 ( (QWidget*)o )->isVisibleTo( this ) &&		 insertedWidgets.find( (QWidget*)o ) && o != w ) {		QRect r2( ( (QWidget*)o )->pos(),			  ( (QWidget*)o )->size() );		if ( r.contains( r2 ) )		    lst.append( (QWidget*)o );	    }	}    }    if ( !lst.isEmpty() ) {	QWidget *pw = WidgetFactory::containerOfWidget( w );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -