summaryrefslogtreecommitdiffstats
path: root/tools/designer/designer/sourcefile.cpp
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2011-07-10 15:24:15 -0500
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2011-07-10 15:24:15 -0500
commitbd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch)
tree7a520322212d48ebcb9fbe1087e7fca28b76185c /tools/designer/designer/sourcefile.cpp
downloadqt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz
qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip
Add Qt3 development HEAD version
Diffstat (limited to 'tools/designer/designer/sourcefile.cpp')
-rw-r--r--tools/designer/designer/sourcefile.cpp302
1 files changed, 302 insertions, 0 deletions
diff --git a/tools/designer/designer/sourcefile.cpp b/tools/designer/designer/sourcefile.cpp
new file mode 100644
index 0000000..370bfd3
--- /dev/null
+++ b/tools/designer/designer/sourcefile.cpp
@@ -0,0 +1,302 @@
+/**********************************************************************
+** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of Qt Designer.
+**
+** This file may be used under the terms of the GNU General
+** Public License versions 2.0 or 3.0 as published by the Free
+** Software Foundation and appearing in the files LICENSE.GPL2
+** and LICENSE.GPL3 included in the packaging of this file.
+** Alternatively you may (at your option) use any later version
+** of the GNU General Public License if such license has been
+** publicly approved by Trolltech ASA (or its successors, if any)
+** and the KDE Free Qt Foundation.
+**
+** Please review the following information to ensure GNU General
+** Public Licensing requirements will be met:
+** http://trolltech.com/products/qt/licenses/licensing/opensource/.
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
+** or contact the sales department at sales@trolltech.com.
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with
+** the Software.
+**
+** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
+** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
+** herein.
+**
+**********************************************************************/
+
+#include "sourcefile.h"
+#include <qfile.h>
+#include <qtextstream.h>
+#include "designerappiface.h"
+#include "sourceeditor.h"
+#include "metadatabase.h"
+#include "../interfaces/languageinterface.h"
+#include <qfiledialog.h>
+#include <qmessagebox.h>
+#include "mainwindow.h"
+#include "workspace.h"
+#include <stdlib.h>
+
+SourceFile::SourceFile( const QString &fn, bool temp, Project *p )
+ : filename( fn ), ed( 0 ), fileNameTemp( temp ),
+ timeStamp( 0, p->makeAbsolute( fn ) ), pro( p ), pkg( FALSE )
+ , accepted( TRUE )
+{
+ iface = 0;
+
+ if ( !temp )
+ accepted = checkFileName( TRUE );
+
+ if (accepted) {
+ load();
+ pro->addSourceFile( this );
+ MetaDataBase::addEntry( this );
+ }
+
+}
+
+SourceFile::~SourceFile()
+{
+ if (iface)
+ delete iface;
+}
+
+QString SourceFile::text() const
+{
+ return txt;
+}
+
+void SourceFile::setText( const QString &s )
+{
+ txt = s;
+}
+
+bool SourceFile::save( bool ignoreModified )
+{
+ if ( fileNameTemp )
+ return saveAs();
+ if ( !ignoreModified && !isModified() )
+ return TRUE;
+ if ( ed )
+ ed->save();
+
+ if ( QFile::exists( pro->makeAbsolute( filename ) ) ) {
+ QString fn( pro->makeAbsolute( filename ) );
+#if defined(Q_OS_WIN32)
+ fn += ".bak";
+#else
+ fn += "~";
+#endif
+ QFile f( pro->makeAbsolute( filename ) );
+ if ( f.open( IO_ReadOnly ) ) {
+ QFile f2( fn );
+ if ( f2.open( IO_WriteOnly | IO_Translate ) ) {
+ QCString data( f.size() );
+ f.readBlock( data.data(), f.size() );
+ f2.writeBlock( data );
+ }
+ }
+ }
+
+ QFile f( pro->makeAbsolute( filename ) );
+ if ( !f.open( IO_WriteOnly | IO_Translate ) )
+ return saveAs();
+
+ QTextStream ts( &f );
+ ts << txt;
+ timeStamp.update();
+ setModified( FALSE );
+ return TRUE;
+}
+
+bool SourceFile::saveAs( bool ignoreModified )
+{
+ LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
+ QString filter;
+ if ( iface )
+ filter = iface->fileFilterList().join(";;");
+
+ QString old = filename;
+ QString initFn = pro->makeAbsolute( filename );
+ if ( ignoreModified ) {
+ QString dir = QStringList::split( ':', project()->iFace()->customSetting( "QTSCRIPT_PACKAGES" ) ).first();
+ initFn = QFileInfo( initFn ).fileName();
+ initFn.prepend( dir + "/" );
+ }
+ QString fn = QFileDialog::getSaveFileName( initFn, filter );
+ if ( fn.isEmpty() )
+ return FALSE;
+ fileNameTemp = FALSE;
+ filename = pro->makeRelative( fn );
+ if ( !checkFileName( TRUE ) ) {
+ filename = old;
+ return FALSE;
+ }
+ pro->setModified( TRUE );
+ timeStamp.setFileName( pro->makeAbsolute( filename ) );
+ if ( ed )
+ ed->setCaption( tr( "Edit %1" ).arg( filename ) );
+ setModified( TRUE );
+ if ( pro->isDummy() ) {
+ QObject *o = ed->parent();
+ while ( o && !o->isA( "MainWindow" ) )
+ o = o->parent();
+ if ( o )
+ ((MainWindow *)o)->addRecentlyOpenedFile( fn );
+ }
+ return save( ignoreModified );
+}
+
+bool SourceFile::load()
+{
+ QFile f( pro->makeAbsolute( filename ) );
+ if ( !f.open( IO_ReadOnly ) )
+ return FALSE;
+ QTextStream ts( &f );
+ txt = ts.read();
+ timeStamp.update();
+ return TRUE;
+}
+
+DesignerSourceFile *SourceFile::iFace()
+{
+ if ( !iface )
+ iface = new DesignerSourceFileImpl( this );
+ return iface;
+}
+
+void SourceFile::setEditor( SourceEditor *e )
+{
+ ed = e;
+}
+
+bool SourceFile::isModified() const
+{
+ if ( !ed )
+ return FALSE;
+ return ed->isModified();
+}
+
+static QMap<QString, int> *extensionCounter;
+QString SourceFile::createUnnamedFileName( const QString &extension )
+{
+ if ( !extensionCounter )
+ extensionCounter = new QMap<QString, int>;
+ int count = -1;
+ QMap<QString, int>::Iterator it;
+ if ( ( it = extensionCounter->find( extension ) ) != extensionCounter->end() ) {
+ count = *it;
+ ++count;
+ extensionCounter->replace( extension, count );
+ } else {
+ count = 1;
+ extensionCounter->insert( extension, count );
+ }
+
+ return "unnamed" + QString::number( count ) + "." + extension;
+}
+
+void SourceFile::setModified( bool m )
+{
+ if ( !ed )
+ return;
+ ed->setModified( m );
+}
+
+bool SourceFile::closeEvent()
+{
+ if ( !isModified() && fileNameTemp ) {
+ pro->removeSourceFile( this );
+ return TRUE;
+ }
+
+ if ( !isModified() )
+ return TRUE;
+
+ if ( ed )
+ ed->save();
+
+ switch ( QMessageBox::warning( MainWindow::self, tr( "Save Code" ),
+ tr( "Save changes to '%1'?" ).arg( filename ),
+ tr( "&Yes" ), tr( "&No" ), tr( "&Cancel" ), 0, 2 ) ) {
+ case 0: // save
+ if ( !save() )
+ return FALSE;
+ break;
+ case 1: // don't save
+ load();
+ if ( ed )
+ ed->editorInterface()->setText( txt );
+ if ( fileNameTemp ) {
+ pro->removeSourceFile( this );
+ return TRUE;
+ }
+ if ( MainWindow::self )
+ MainWindow::self->workspace()->update();
+ break;
+ case 2: // cancel
+ return FALSE;
+ default:
+ break;
+ }
+ setModified( FALSE );
+ return TRUE;
+}
+
+bool SourceFile::close()
+{
+ if ( !ed )
+ return TRUE;
+ return ed->close();
+}
+
+Project *SourceFile::project() const
+{
+ return pro;
+}
+
+void SourceFile::checkTimeStamp()
+{
+ if ( timeStamp.isUpToDate() )
+ return;
+ timeStamp.update();
+ if ( QMessageBox::information( MainWindow::self, tr( "Qt Designer" ),
+ tr( "File '%1' has been changed outside Qt Designer.\n"
+ "Do you want to reload it?" ).arg( filename ),
+ tr( "&Yes" ), tr( "&No" ) ) == 0 ) {
+ load();
+ if ( ed )
+ ed->editorInterface()->setText( txt );
+ }
+}
+
+bool SourceFile::checkFileName( bool allowBreak )
+{
+ SourceFile *sf = pro->findSourceFile( filename, this );
+ if ( sf )
+ QMessageBox::warning( MainWindow::self, tr( "Invalid Filename" ),
+ tr( "The project already contains a source file with \n"
+ "filename '%1'. Please choose a new filename." ).arg( filename ) );
+ while ( sf ) {
+ LanguageInterface *iface = MetaDataBase::languageInterface( pro->language() );
+ QString filter;
+ if ( iface )
+ filter = iface->fileFilterList().join(";;");
+ QString fn;
+ while ( fn.isEmpty() ) {
+ fn = QFileDialog::getSaveFileName( pro->makeAbsolute( filename ), filter );
+ if ( allowBreak && fn.isEmpty() )
+ return FALSE;
+ }
+ filename = pro->makeRelative( fn );
+ sf = pro->findSourceFile( filename, this );
+ }
+ return TRUE;
+}