[kst-plot] /: script server client name handling

Barth Netterfield null at kde.org
Fri Dec 1 13:55:26 UTC 2017


Git commit 94d2cc550be554e34f84209df277e6b692021c5e by Barth Netterfield.
Committed on 01/12/2017 at 13:55.
Pushed by netterfield into branch 'master'.

script server client name handling

Display script server client name in the title bar.
Save and load script server client name in .kst files.
Include instructions for setting client name in --help.

M  +13   -5    docbook/kst/tutorial.docbook
M  +5    -3    src/libkstapp/commandlineparser.cpp
M  +8    -0    src/libkstapp/document.cpp
M  +36   -5    src/libkstapp/mainwindow.cpp
M  +5    -0    src/libkstapp/mainwindow.h
M  +55   -10   src/libkstapp/scriptserver.cpp
M  +4    -1    src/libkstapp/scriptserver.h

https://commits.kde.org/kst-plot/94d2cc550be554e34f84209df277e6b692021c5e

diff --git a/docbook/kst/tutorial.docbook b/docbook/kst/tutorial.docbook
index fda5cad7..7c3ea6fc 100644
--- a/docbook/kst/tutorial.docbook
+++ b/docbook/kst/tutorial.docbook
@@ -742,13 +742,13 @@ Then, from the <guimenu>Create</guimenu> menu select
 
 <para>
   It is hard to see any details in this plot - a log-log plot would be
-  much easier. To convert the Y axis to log hit <keycap>L</keycap> and
-  to convert the X axis to log hit <keycap>G</keycap>.  (These options
+  much easier. To toggle the Y axis to log hit <keycap>L</keycap> and
+  to toggle the X axis to log hit <keycap>G</keycap>.  (These options
   are also available in the context menu under the 
   <guimenuitem>Zoom</guimenuitem> submenu in case you forget the keyboard
   commands).  The plot will now be log-log, as shown:
 </para>
-
+ 
 <screenshot>
 <screeninfo>Spectrum</screeninfo>
 <mediaobject>
@@ -762,8 +762,16 @@ Then, from the <guimenu>Create</guimenu> menu select
 </screenshot>
 
 <para>
-  From the <guimenu>Create</guimenu> you can similarly create histograms, spectograms,
-  and equations of fields you have loaded.
+  Hitting <keycap>L</keycap> and <keycap>G</keycap> will toggle back to linear axis.
+</para>
+
+<para>
+  From the <guimenu>Create</guimenu> you can similarly create histograms, spectrograms and equations
+  of fields you have loaded.
+</para>
+  
+<para>
+  Save your session - we will be using it again in the next section of the tutorial.
 </para>
 </sect1>
 
diff --git a/src/libkstapp/commandlineparser.cpp b/src/libkstapp/commandlineparser.cpp
index ebc257aa..6c503f34 100644
--- a/src/libkstapp/commandlineparser.cpp
+++ b/src/libkstapp/commandlineparser.cpp
@@ -43,8 +43,8 @@ namespace Kst {
   static const char *usageMessage =
 "KST Command Line Usage\n"
 "************************\n"
-"*** Load a kst file: ***\n"
-"kst [OPTIONS] kstfile\n"
+"*** Load a kst session: ***\n"
+"kst [OPTIONS] <kstfile>\n"
 "\n"
 "[OPTIONS] will override the datasource parameters for all data sources in the kst file:\n"
 "      -F  <datasource>\n"
@@ -56,7 +56,7 @@ namespace Kst {
 
   static const char *usageDetailsMessage =
 "*** Read a data file ***\n"
-"kst datasource OPTIONS [datasource OPTIONS []]\n"
+"kst [<datasource> [OPTIONS]]\n"
 "\n"
 "OPTIONS are read and interpreted in order. Except for data object options, all are applied to all future data objects, unless later overridden.\n"
 "Output Options:\n"
@@ -107,6 +107,8 @@ namespace Kst {
 "      -p <field>               plot the spectrum of field.\n"
 "      -h <field>               plot a histogram of field.\n"
 "      -z <field>               plot an image of matrix field.\n"
+"Misc:\n"
+"      --clientName=<client name> Set the client name for connecting with a script.\n"
 "\n"
 "****************\n"
 "*** Examples ***\n"
diff --git a/src/libkstapp/document.cpp b/src/libkstapp/document.cpp
index 0d1c1441..a8da906d 100644
--- a/src/libkstapp/document.cpp
+++ b/src/libkstapp/document.cpp
@@ -136,6 +136,9 @@ bool Document::save(const QString& to) {
   } else {
     xml.writeAttribute("version", objectStore()->sessionVersionString);
   }
+  if (_win->scriptServerNameSet()) {
+    xml.writeAttribute("clientName", _win->scriptServerName());
+  }
 
   xml.writeStartElement("data");
   foreach (DataSourcePtr s, objectStore()->dataSourceList()) {
@@ -271,6 +274,11 @@ bool Document::open(const QString& file) {
         if (version.size()>2) {
           objectStore()->sessionVersion += version[2].toInt();
         }
+        QString server_name = attrs.value("clientName").toString();
+        if (!server_name.isEmpty()) {
+          _win->setScriptServerName(server_name);
+        }
+
         //qDebug() << "version" << version << objectStore()->sessionVersion;
       } else if (n == "data") {
         if (state != Unknown) {
diff --git a/src/libkstapp/mainwindow.cpp b/src/libkstapp/mainwindow.cpp
index 8113cfc4..80be9fc2 100644
--- a/src/libkstapp/mainwindow.cpp
+++ b/src/libkstapp/mainwindow.cpp
@@ -111,7 +111,8 @@ MainWindow::MainWindow() :
     _ae_display(2),
     _ae_export_all(false),
     _ae_autosave_period(0),
-    _ae_Timer(0)
+    _ae_Timer(0),
+    _sessionFileName(QString())
 {
   _doc = new Document(this);
   _scriptServer = new ScriptServer(_doc->objectStore());
@@ -122,7 +123,7 @@ MainWindow::MainWindow() :
   _debugDialog = new DebugDialog(this);
   Debug::self()->setHandler(_debugDialog);
 
-  setWindowTitle("Kst");
+  setKstWindowTitle();
 
   createActions();
   createMenus();
@@ -300,7 +301,8 @@ void MainWindow::saveAs() {
   //QString currentP = QDir::currentPath();
   _doc->save(fn);
   QDir::setCurrent(restorePath);
-  setWindowTitle("Kst - " + fn);
+  _sessionFileName = fn;
+  setKstWindowTitle();
   updateRecentKstFiles(fn);
 }
 
@@ -366,6 +368,33 @@ void MainWindow::updateRecentKstFiles(const QString& filename)
   updateRecentFiles("recentKstFileList", _fileMenu, _bottomRecentKstActions, _recentKstFilesMenu, filename, SLOT(openRecentKstFile()));
 }
 
+void MainWindow::setKstWindowTitle()
+{
+  QString title = "Kst";
+
+  if (!_sessionFileName.isEmpty()) {
+    title += " - " + _sessionFileName;
+  }
+  title += " -- " + _scriptServer->serverName;
+  setWindowTitle(title);
+}
+
+QString MainWindow::scriptServerName()
+{
+  return _scriptServer->serverName;
+}
+
+bool MainWindow::scriptServerNameSet()
+{
+  return _scriptServer->serverNameSet;
+}
+
+void MainWindow::setScriptServerName(QString server_name)
+{
+  _scriptServer->setScriptServerName(server_name);
+  setKstWindowTitle();
+}
+
 void MainWindow::copyTab()
 {
   View *view = _tabWidget->currentView();
@@ -526,7 +555,8 @@ bool MainWindow::initFromCommandLine() {
     ok = false;
   }
   if (!P.kstFileName().isEmpty()) {
-    setWindowTitle("Kst - " + P.kstFileName());
+    _sessionFileName = P.kstFileName();
+    setKstWindowTitle();
   }
   _doc->setChanged(false);
   return ok;
@@ -547,7 +577,8 @@ void MainWindow::openFile(const QString &file) {
     QMessageBox::critical(this, tr("Kst"),tr("Error opening document:\n  '%1'\n%2\n").arg(file, lastError));
   }
 
-  setWindowTitle("Kst - " + file);
+  _sessionFileName = file;
+  setKstWindowTitle();
   updateRecentKstFiles(file);
 }
 
diff --git a/src/libkstapp/mainwindow.h b/src/libkstapp/mainwindow.h
index 66f6cf20..8fdd6b8e 100644
--- a/src/libkstapp/mainwindow.h
+++ b/src/libkstapp/mainwindow.h
@@ -73,6 +73,10 @@ class MainWindow : public QMainWindow
     static void setWidgetFlags(QWidget*);
     void updateRecentKstFiles(const QString& newfilename = QString());
 
+    void setKstWindowTitle();
+    QString scriptServerName();
+    bool scriptServerNameSet();
+    void setScriptServerName(QString server_name);
 
   public Q_SLOTS:
     void copyTab();
@@ -365,6 +369,7 @@ class MainWindow : public QMainWindow
     bool _ae_export_all;
     int _ae_autosave_period;
     QTimer *_ae_Timer;
+    QString _sessionFileName;
 
     friend class ScriptServer;
 };
diff --git a/src/libkstapp/scriptserver.cpp b/src/libkstapp/scriptserver.cpp
index 7ee2f69a..00cd4241 100644
--- a/src/libkstapp/scriptserver.cpp
+++ b/src/libkstapp/scriptserver.cpp
@@ -72,31 +72,54 @@ namespace Kst {
 
 ScriptServer::ScriptServer(ObjectStore *obj) : _server(new QLocalServer(this)), _store(obj),_interface(0) {
 
-    QString initial="kstScript";
+    QString initial;
 
+    // get the Username from the OS.  On windows this is USERNAME and on linux, USER.
+#ifdef Q_OS_WIN
+    initial = qgetenv("USERNAME");
+    if (initial.isEmpty())  {// hmmm... something odd.
+      initial = qgetenv("USER");
+    }
+#else
+    initial = qgetenv("USER");
+    if (initial.isEmpty()) { // hmmm... something odd.
+      initial = qgetenv("USERNAME");
+    }
+#endif
+    if (initial.isEmpty()) {
+      initial = "KstScript";
+    }
 
+    serverName = false;
     // The command line hasn't been parsed yet, so
     // we can't rely on that to get the server name.
     QStringList args= qApp->arguments();
     for(int i=0;i<args.size();i++) {
-        if(args.at(i).startsWith("--serverName=")) {
+        if(args.at(i).startsWith("--clientName=")) {
             initial=args.at(i);
-            initial.remove("--serverName=");
-        }
+            initial.remove("--clientName=");
+            serverNameSet = true;
+        } else if(args.at(i).startsWith("--serverName=")) {
+          initial=args.at(i);
+          initial.remove("--serverName=");
+          serverNameSet = true;
+      }
     }
 
-    QString connectTo=initial;
+    serverName=initial;
+
+    int j = 1;
     while(1) {
         QLocalSocket socket;
-        socket.connectToServer(connectTo);
+        socket.connectToServer(serverName);
         socket.waitForConnected(300);
         if(socket.state()!=QLocalSocket::ConnectedState) {
-            _server->removeServer(connectTo);
-            _server->listen(connectTo);
+            _server->removeServer(serverName);
+            _server->listen(serverName);
             break;
         }
         socket.disconnectFromServer();
-        connectTo=initial+QString::number(connectTo.remove(initial).toInt()+1);
+        serverName=initial+"-"+QString::number(j++);
     }
     connect(_server,SIGNAL(newConnection()),this,SLOT(procConnection()));
 
@@ -241,7 +264,29 @@ ScriptServer::ScriptServer(ObjectStore *obj) : _server(new QLocalServer(this)),
 ScriptServer::~ScriptServer()
 {
     delete _server;
-    delete _interface;
+  delete _interface;
+}
+
+void ScriptServer::setScriptServerName(QString initial)
+{
+  int j = 1;
+
+  _server->close();
+  serverName = initial;
+  while(1) {
+      QLocalSocket socket;
+      socket.connectToServer(serverName);
+      socket.waitForConnected(300);
+      if(socket.state()!=QLocalSocket::ConnectedState) {
+          _server->removeServer(serverName);
+          _server->listen(serverName);
+          break;
+      }
+      socket.disconnectFromServer();
+      serverName=initial+"-"+QString::number(j++);
+  }
+  serverNameSet = true;
+  //connect(_server,SIGNAL(newConnection()),this,SLOT(procConnection()));
 }
 
 /** Conv. function which takes a response, and executes if 'if' statement is unexistant or true. */
diff --git a/src/libkstapp/scriptserver.h b/src/libkstapp/scriptserver.h
index e2586a21..697bbb49 100644
--- a/src/libkstapp/scriptserver.h
+++ b/src/libkstapp/scriptserver.h
@@ -42,8 +42,11 @@ class ScriptServer : public QObject
 public:
     explicit ScriptServer(ObjectStore*obj);
     ~ScriptServer();
-    //QByteArray checkPrimatives(QByteArray&command,QLocalSocket* s);
     void setStore(ObjectStore *obj) { _store = obj; vi.clear();}
+    QString serverName;
+    bool serverNameSet;
+    void setScriptServerName(QString server_name);
+
 public slots:
     void procConnection();
     void readSomething();


More information about the kde-doc-english mailing list