[Kstars-devel] KDE/kdeedu/kstars/kstars/tools

Jason Harris kstars at 30doradus.org
Sat Dec 22 22:10:07 CET 2007


SVN commit 751792 by harris:

More ScriptBuilder fixes:

Don't ask for confirmation when saving a script that had been
opened or previously saved.

Parse qouted strings in arguments of script functions properly
(someone had wrapped this code in "#if 0", so please verify)

Don't disable the "Save As" button after a save.  "Save" gets
disabled to signal that no unsaved changes have been made.
The "Save As" button should be enabled as long as the current
script is not empty.

Don't signal unsaved changes if a function is simply selected in
the function list

When the last function in the script is removed, make the
previous function the selected one.

Update the argument widget whenever the selected function changes.

Discard the script when the scriptbuilder window is closed.

make sure string arguments are quoted when writing the script

Don't show "I18n_EMPTY_MESSAGE" in province field of the
setGeoLocation argument widget, if the province of the selected
city is undefined (just leave it blank)

CCMAIL: kstars-devel at kde.org



 M  +98 -75    scriptbuilder.cpp  
 M  +1 -0      scriptbuilder.h  
 M  +16 -1     scriptfunction.cpp  


--- trunk/KDE/kdeedu/kstars/kstars/tools/scriptbuilder.cpp #751791:751792
@@ -128,7 +128,7 @@
 }
 
 ScriptBuilder::ScriptBuilder( QWidget *parent )
-        : KDialog( parent ), UnsavedChanges(false),
+        : KDialog( parent ), UnsavedChanges(false), checkForChanges(true),
         currentFileURL(), currentDir( QDir::homePath() ),
         currentScriptName(), currentAuthor()
 {
@@ -938,6 +938,7 @@
         sb->CopyButton->setEnabled( false );
         sb->RemoveButton->setEnabled( false );
         sb->RunButton->setEnabled( false );
+        sb->SaveAsButton->setEnabled( false );
 
         currentFileURL = QString();
         currentScriptName = QString();
@@ -1005,8 +1006,11 @@
         }
     }
 
-    if ( currentFileURL.isEmpty() )
+    bool newFilename = false;
+    if ( currentFileURL.isEmpty() ) {
         currentFileURL = KFileDialog::getSaveUrl( currentDir, "*.kstars|KStars Scripts (*.kstars)" );
+        newFilename = true;
+    }
 
     if ( currentFileURL.isValid() ) {
         currentDir = currentFileURL.directory();
@@ -1015,7 +1019,7 @@
             fname = currentFileURL.path();
 
             //Warn user if file exists
-            if (QFile::exists(currentFileURL.path())) {
+            if (newFilename == true && QFile::exists(currentFileURL.path())) {
                 int r=KMessageBox::warningContinueCancel(static_cast<QWidget *>(parent()),
                         i18n( "A file named \"%1\" already exists. "
                               "Overwrite it?" , currentFileURL.fileName()),
@@ -1197,7 +1201,7 @@
         line = istream.readLine();
 
         //look for name of script
-        if ( line.contains( "#KStars DCOP script: " ) )
+        if ( line.contains( "#KStars DBus script: " ) )
             currentScriptName = line.mid( 21 ).trimmed();
 
         //look for author of scriptbuilder
@@ -1253,7 +1257,7 @@
     // clean up the string list first if needed
     // We need to perform this in case we havea quoted string "NGC 3000" because this will counted
     // as two arguments, and it should be counted as one.
-    //bool foundQuote(false), quoteProcessed(false);
+    bool foundQuote(false), quoteProcessed(false);
     QString cur, arg;
     QStringList::iterator it;
 
@@ -1264,95 +1268,92 @@
         cur = cur.mid(cur.indexOf(":") + 1).remove("'");
 
         (*it) = cur;
-    }
 
-	#if 0
-    if ( cur.startsWith('\"'))
-    {
-        arg += cur.right(cur.length() - 1);
-        arg += ' ';
-        foundQuote = true;
-        quoteProcessed = true;
+        if ( cur.startsWith('\"'))
+        {
+            arg += cur.right(cur.length() - 1);
+            arg += ' ';
+            foundQuote = true;
+            quoteProcessed = true;
+        }
+        else if (cur.endsWith('\"'))
+        {
+            arg += cur.left(cur.length() -1);
+            arg += '\'';
+            foundQuote = false;
+        }
+        else if (foundQuote)
+        {
+            arg += cur;
+            arg += ' ';
+        }
+        else
+        {
+            arg += cur;
+            arg += '\'';
+        }
     }
-    else if (cur.endsWith('\"'))
-    {
-        arg += cur.left(cur.length() -1);
-        arg += '\'';
-        foundQuote = false;
-    }
-    else if (foundQuote)
-    {
-        arg += cur;
-        arg += ' ';
-    }
-    else
-    {
-        arg += cur;
-        arg += '\'';
-    }
-}
 
-if (quoteProcessed)
-    fn = arg.split( "'" );
+    if (quoteProcessed)
+        fn = arg.split( "'", QString::SkipEmptyParts );
 
-	#endif
-
-//loop over known functions to find a name match
-foreach ( ScriptFunction *sf, KStarsFunctionList )
-{
-    if ( fn_name == sf->name() )
+    //loop over known functions to find a name match
+    foreach ( ScriptFunction *sf, KStarsFunctionList )
     {
+        if ( fn_name == sf->name() )
+        {
 
-        if ( fn_name == "setGeoLocation" )
-        {
-            QString city( fn[0] ), prov, cntry( fn[1] );
-            prov.clear();
-            if ( fn.count() == 4 ) { prov = fn[1]; cntry = fn[2]; }
-            if ( fn.count() == 3 || fn.count() == 4 )
+            if ( fn_name == "setGeoLocation" )
             {
-                ScriptList.append( new ScriptFunction( sf ) );
-                ScriptList.last()->setArg( 0, city );
-                ScriptList.last()->setArg( 1, prov );
-                ScriptList.last()->setArg( 2, cntry );
-            } else return false;
+                QString city( fn[0] ), prov, cntry( fn[1] );
+                prov.clear();
+                if ( fn.count() == 4 ) { prov = fn[1]; cntry = fn[2]; }
+                if ( fn.count() == 3 || fn.count() == 4 )
+                {
+                    ScriptList.append( new ScriptFunction( sf ) );
+                    ScriptList.last()->setArg( 0, city );
+                    ScriptList.last()->setArg( 1, prov );
+                    ScriptList.last()->setArg( 2, cntry );
+                } else return false;
 
-        } else if ( fn.count() != sf->numArgs()) return false;
+            } else if ( fn.count() != sf->numArgs()) return false;
 
-        ScriptList.append( new ScriptFunction( sf ) );
+            ScriptList.append( new ScriptFunction( sf ) );
 
-        for ( int i=0; i<sf->numArgs(); ++i )
-            ScriptList.last()->setArg( i, fn[i] );
+            for ( int i=0; i<sf->numArgs(); ++i )
+                ScriptList.last()->setArg( i, fn[i] );
 
-        return true;
-    }
+            return true;
+        }
 
-		#if 0
-    foreach ( ScriptFunction *sf, INDIFunctionList )
-    {
-        if ( fn[0] == sf->name() )
+        #if 0
+        foreach ( ScriptFunction *sf, INDIFunctionList )
         {
+            if ( fn[0] == sf->name() )
+            {
 
-            if ( fn.count() != sf->numArgs() + 1 ) return false;
+                if ( fn.count() != sf->numArgs() + 1 ) return false;
 
-            ScriptList.append( new ScriptFunction( sf ) );
+                ScriptList.append( new ScriptFunction( sf ) );
 
-            for ( int i=0; i<sf->numArgs(); ++i )
-                ScriptList.last()->setArg( i, fn[i+1] );
+                for ( int i=0; i<sf->numArgs(); ++i )
+                    ScriptList.last()->setArg( i, fn[i+1] );
 
-            return true;
+                return true;
+            }
         }
+        #endif
     }
-		#endif
-}
 
-//if we get here, no function-name match was found
-return false;
+    //if we get here, no function-name match was found
+    return false;
 }
 
 void ScriptBuilder::setUnsavedChanges( bool b ) {
-    UnsavedChanges = b;
-    sb->SaveButton->setEnabled( b );
-    sb->SaveAsButton->setEnabled( b );
+    if ( checkForChanges ) {
+        UnsavedChanges = b;
+        sb->SaveButton->setEnabled( b );
+    }
 }
 
 void ScriptBuilder::slotCopyFunction() {
@@ -1367,6 +1368,7 @@
     sb->ScriptListBox->insertItem( Pos, ScriptList[Pos]->name());
     //sb->ScriptListBox->setSelected( Pos, true );
     sb->ScriptListBox->setCurrentRow(Pos);
+    slotArgWidget();
 }
 
 void ScriptBuilder::slotRemoveFunction() {
@@ -1379,10 +1381,16 @@
         sb->ArgStack->setCurrentWidget( argBlank );
         sb->CopyButton->setEnabled( false );
         sb->RemoveButton->setEnabled( false );
+        sb->RunButton->setEnabled( false );
+        sb->SaveAsButton->setEnabled( false );
     } else {
         //sb->ScriptListBox->setSelected( Pos, true );
+        if ( Pos == sb->ScriptListBox->count() ) {
+            Pos = Pos - 1;
+        }
         sb->ScriptListBox->setCurrentRow(Pos);
     }
+    slotArgWidget();
 }
 
 void ScriptBuilder::slotAddFunction() {
@@ -1438,6 +1446,7 @@
         sb->ScriptListBox->takeItem( n );
         sb->ScriptListBox->insertItem( n-1, t);
         sb->ScriptListBox->setCurrentRow(n-1);
+        slotArgWidget();
     }
 }
 
@@ -1455,6 +1464,7 @@
         sb->ScriptListBox->takeItem( n );
         sb->ScriptListBox->insertItem( n+1 , t);
         sb->ScriptListBox->setCurrentRow( n+1);
+        slotArgWidget();
     }
 }
 
@@ -1487,11 +1497,13 @@
         sb->DownButton->setEnabled( true );
     }
 
-    //sb->RunButton enabled when script not empty.
+    //RunButton and SaveAs button enabled when script not empty.
     if ( sb->ScriptListBox->count() ) {
         sb->RunButton->setEnabled( true );
+        sb->SaveAsButton->setEnabled( true );
     } else {
         sb->RunButton->setEnabled( false );
+        sb->SaveAsButton->setEnabled( false );
         setUnsavedChanges( false );
     }
 
@@ -1502,6 +1514,8 @@
         unsigned int n = sb->ScriptListBox->currentRow();
         ScriptFunction *sf = ScriptList.at( n );
 
+        checkForChanges = false; //Don't signal unsaved changes
+
         if ( sf->name() == "lookTowards" ) {
             sb->ArgStack->setCurrentWidget( argLookToward );
             QString s = sf->argVal(0);
@@ -1865,6 +1879,8 @@
 
         }
 		 #endif
+
+        checkForChanges = true; //signal unsaved changes if the argument widgets are changed
     }
 }
 
@@ -1913,7 +1929,11 @@
         if ( ld.selectedCity() ) {
             // set new location names
             argSetGeoLocation->CityName->setText( ld.selectedCityName() );
-            argSetGeoLocation->ProvinceName->setText( ld.selectedProvinceName() );
+            if ( ! ld.selectedProvinceName().isEmpty() ) {
+                argSetGeoLocation->ProvinceName->setText( ld.selectedProvinceName() );
+            } else {
+                argSetGeoLocation->ProvinceName->clear();
+            }
             argSetGeoLocation->CountryName->setText( ld.selectedCountryName() );
 
             ScriptFunction *sf = ScriptList[ sb->ScriptListBox->currentRow() ];
@@ -2353,9 +2373,12 @@
 {
     saveWarning();
 
-    if ( !UnsavedChanges )
+    if ( !UnsavedChanges ) {
+        ScriptList.clear();
+        sb->ScriptListBox->clear();
+        sb->ArgStack->setCurrentWidget( argBlank );
         close();
-
+    }
 }
 
 //TODO JM: INDI Scripting to be included in KDE 4.1
--- trunk/KDE/kdeedu/kstars/kstars/tools/scriptbuilder.h #751791:751792
@@ -227,6 +227,7 @@
     QTreeWidgetItem *opsGUI, *opsToolbar, *opsShowObj, *opsShowOther, *opsCName, *opsHide, *opsSkymap, *opsLimit;
 
     bool UnsavedChanges;
+    bool checkForChanges;
     KUrl currentFileURL;
     QString currentDir;
     QString currentScriptName, currentAuthor;
--- trunk/KDE/kdeedu/kstars/kstars/tools/scriptfunction.cpp #751791:751792
@@ -187,8 +187,23 @@
 
     while ( ! ArgName[i].isEmpty() && i < 6 )
     {
+        //Make sure strings are quoted
+        QString value = ArgVal[i];
+        if ( ArgDBusType[i] == "string" ) {
+            if ( value.isEmpty() ) {
+                value = "\"\"";
+            } else {
+                if ( value.left(1) != "\"" && value.left(1) != "\'" ) {
+                    value = '\"' + value;
+                }
+                if ( value.right(1) != "\"" && value.right(1) != "\'" ) {
+                    value =  value + '\"';
+                }
+            }
+        }
+
         // Write DBus style prototype compatible with dbus-send format
-        out += " " + ArgDBusType[i] + ":" + ArgVal[i];
+        out += " " + ArgDBusType[i] + ":" + value;
         ++i;
     }
 


More information about the Kstars-devel mailing list