/****************************************************************/
/*                                                              */
/*      demo1.cpp                                               */
/*                                                              */
/*      This file is a part of kpl - a program for graphical    */
/*      presentation of data sets and functions.                */
/*                                                              */
/*      Copyright (C) 2010 by Werner Stille                     */
/*      <stille@uni-freiburg.de>                                */
/*                                                              */
/*      Released under the GPL; see file LICENSE for details.   */
/*                                                              */
/****************************************************************/

#include <iostream>
#include "kpl_interface.h"

using namespace std;

const char* test(const QDBusPendingReply<bool>& a) {
  static const char t[] = "true ";
  static const char f[] = "false ";
  QDBusReply<bool> reply = a;
  return (reply.isValid() && reply.value()) ? t : f;
}

int main()
{
  QString prefix("/usr/share/kde4/apps/kpl/");
  org::kde::Kpl iface("org.kde.Kpl", "/Kpl", QDBusConnection::sessionBus());
  cerr << "Autoplot settings\n";
  iface.setAutoplot(4, 15, 3, 10, 2, "x", "y", "", true, 0, 1, 2, false,
                    false, false, 1, true, false, 1);
  cerr << "Open data file... " << test(iface.openData(prefix + "demo1.dat"));
  sleep(3);
  cerr << "\nOpen plot file... " << test(iface.openPlot(prefix + "demo5.plo"));
  sleep(3);
  cerr << "\nDeleting items... ";
  cerr << test(iface.deleteItem(8));
  cerr << test(iface.deleteItem(4));
  cerr << test(iface.deleteItem(6));
  cerr << test(iface.deleteItem(3));
  cerr << test(iface.deleteItem(4));
  cerr << test(iface.deleteItem(2));
  sleep(3);
  cerr << "\nNew plot\n";
  iface.newPlot();
  cerr << "New frame item\n";
  iface.newFrameItem(4, 15, 3, 10, 0, 12.5664, -2, 2, false, false, 2, -1,
                     "0", "0", 3.14159, 1, 2, 2, 2, 0, 0, "x", "y",
                     "Fourier synthesis", 1, 0, 0, true, false, true,
                     false, false, true);
  cerr << "New function item... "
       << test(iface.newFunItem(prefix + "fkt.so", "foursynth", 0, 12.566,
                                0.01, 1, "ff", 1, 1, 0, 0, 0, 1, false));
  cerr << "\nSetting parameter... " << test(iface.setPar(1, 3, 0, 1));
  cerr << "\nPerforming plot... " << test(iface.activateItem(1, true));
  sleep(1);
  cerr << "\nChanging parameters... ";
  for (int i = 0; i < 11; ++i)
    cerr << test(iface.setPar(1, 3, i * 0.1, 1));
  sleep(2);
  cerr << test(iface.setPar(1, 7, -0.11111111, 1))
       << test(iface.setPar(1, 11, 0.04, 1))
       << test(iface.setPar(1, 15, -0.0204082, 1))
       << test(iface.setPar(1, 19, 0.01234567, 1));
  sleep(3);
  cerr << "\nNew plot";
  iface.newPlot();
  cerr << "\nNew frame item";
  iface.newFrameItem(4, 10, 3, 10, -1, 1, -1, 1, 0, 0, 1, 1, "0", "0", 0.5,
                     0.5, 5, 5, 2, 0, 0, "x", "y", "Parametric function", 1.5,
                     0, 0, true, false, true, false, false, true);
  cerr << "\nNew parametric function item... "
       << test(iface.newParFunItem(prefix + "fkt.so", "foursynth",
                                   prefix + "fkt.so", "foursynth", 0, 6.28318,
                                   0, 1, "ff", 1, 1, 0, 0, 0, 1, true));
  cerr << "\nChanging parameters... ";
  for (int i = 0; i < 10; ++i)
    cerr << test(iface.setPar(1, 2, i * 0.1, 1))
         << test(iface.setPar(1, 7, i * 0.1, 0));
  sleep(3);
  cerr << "\nNew plot";
  iface.newPlot();
  cerr << "\nNew autoframe item";
  iface.newAutoFrameItem();
  cerr << "\nNew array item... "
       << test(iface.newArrayItem(prefix + "demo7.dat", 0, 1, 2, 0, 51, true,
                                  0, "0", 1, 1, 0, 0, 0, 0, 1, true));
  cerr << "\nNew array item... "
       << test(iface.newArrayItem(prefix + "demo7.dat", 0, 3, 4, 0, 51, true,
                                  0, "0", 1, 1, 0, 0, 0, 0, 1, true));
  cerr << "\nAutoscaling... "
       << test(iface.autoScaleFrame(0, 1));
  cerr << "\nNew function item... "
       << test(iface.newFunItem(prefix + "fcompl.so", "epsilon_1", 2, 7, 0, 1,
                                "0", 1, 1, 0, 0, 0, 1, false));
  cerr << "\nChanging parameters... "
       << test(iface.setPar(3, 0, 3.5, 1))
       << test(iface.setPar(3, 1, 8.5, 1))
       << test(iface.setPar(3, 2, 7500, 1))
       << test(iface.setPar(3, 3, 1, 1))
       << test(iface.setPar(3, 4, 150000, 1));
  cerr << "\nPerforming plot... "
       << test(iface.activateItem(3, true));
  cerr << "\nNew function item... "
       << test(iface.newFunItem(prefix + "fcompl.so", "epsilon_2", 2, 7, 0, 1,
                                "ff", 1, 1, 0, 0, 0, 1, false));
  cerr << "\nChanging parameters... "
       << test(iface.setPar(4, 0, 3.5, 1))
       << test(iface.setPar(4, 1, 8.5, 1))
       << test(iface.setPar(4, 2, 7500, 1))
       << test(iface.setPar(4, 3, 1, 1))
       << test(iface.setPar(4, 4, 150000, 1));
  cerr << "\nPerforming plot... "
       << test(iface.activateItem(4, true));
  cerr << "\nEnabling fit parameters... ";
  for (int i = 0; i < 20; ++i)
    cerr << test(iface.enableFitPar(i, (i < 5)));
  cerr << "\nAdding fit items... ";
  cerr << test(iface.addFitItems(true, 1, 3, true, "", ""));
  cerr << test(iface.addFitItems(false, 2, 4, true, "", ""));
  sleep(3);
  cerr << "\nPerforming fit... "
       << test(iface.fit(true, false, false));
  cerr << "\nchi-square = ";
  QDBusReply<double> reply = iface.chiSquare();
  cerr << reply.value();
  sleep(3);
  cerr << "\nNew plot";
  iface.newPlot();
  cerr << "\nNew text item\n";
  iface.newTextItem("#BNThe End", "cc6600", 1, 3.2, 7.5, 0, 5, true);
  return 0;
}
