#!/usr/bin/python
##################################################################
#                                                                #
#       demo1.py                                                 #
#                                                                #
#       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.    #
#                                                                #
##################################################################
#
# Example script for Kpl.
#
# To run it, you need to have installed dbus-python
#

import os, dbus
from time import sleep

kdedir = "/usr"
prefix = kdedir + "/share/kde4/apps/kpl/"
if (not os.path.exists(prefix + "demo1.dat")):
  print prefix + "demo1.dat not found, let's try /usr/share/kde"
  kdedir = "/usr"
  prefix = kdedir + "/share/kde/apps/kpl/"

iface = dbus.Interface(dbus.SessionBus().get_object("org.kde.Kpl", "/Kpl"),
                                                    "org.kde.Kpl")
print "Autoplot settings"
iface.setAutoplot(4.0, 15.0, 3.0, 10.0, 2, "x", "y", "", True, 0, 1, 2, False,
                  False, False, 1, True, False, 1)
print "Open data file...",
if iface.openData(prefix + "demo1.dat"):
  print "true"
else:
  print "false"
sleep(3)
print "Open plot file...",
if iface.openPlot(prefix + "demo5.plo"):
  print "true"
else:
  print "false"
print "Deleting items...",
sleep(2)
if iface.deleteItem(8):
  print "true",
else:
  print "false",
if iface.deleteItem(4):
  print "true",
else:
  print "false",
if iface.deleteItem(6):
  print "true",
else:
  print "false",
if iface.deleteItem(3):
  print "true",
else:
  print "false",
if iface.deleteItem(4):
  print "true",
else:
  print "false",
if iface.deleteItem(2):
  print "true"
else:
  print "false"
sleep(3)
print "New plot";
iface.newPlot()
print "New frame item"
iface.newFrameItem(4.0, 15.0, 3.0, 10.0, 0.0, 12.5664, -2.0, 2.0, False, False,
                   2, -1, "0", "0", 3.14159, 1.0, 2, 2, 2, 0, 0, "x", "y",
                   "Fourier synthesis", 1.0, 0.0, 0.0, True, False, True,
                    False, False, True)
print "New function item...",
if iface.newFunItem(prefix + "fkt.so", "foursynth", 0.0, 12.566, 0.01, 1, "ff",
                    1.0, 1.0, 0, 0.0, 0.0, 1.0, False):
  print "true"
else:
  print "false"
print "Setting parameter...",
if iface.setPar(1, 3, 0.0, 1):
  print "true"
else:
  print "false"
print "Performing plot...",
if iface.activateItem(1, True):
  print "true"
else:
  print "false"
sleep(1)
print "Changing parameters...",
for i in range(11):
  if iface.setPar(1, 3, i * 0.1, 1):
    print "true",
  else:
    print "false",
sleep(2)
if iface.setPar(1, 7, -0.11111111, 1):
  print "true",
else:
  print "false",
if iface.setPar(1, 11, 0.04, 1):
  print "true",
else:
  print "false",
if iface.setPar(1, 15, -0.0204082, 1):
  print "true",
else:
  print "false",
if iface.setPar(1, 19, 0.01234567, 1):
  print "true"
else:
  print "false"
sleep(3)
print "New plot"
iface.newPlot()
print "New frame item"
iface.newFrameItem(4.0, 10.0, 3.0, 10.0, -1.0, 1.0, -1.0, 1.0, 0, 0, 1, 1,
                   "0", "0", 0.5, 0.5, 5, 5, 2, 0, 0, "x", "y",
                   "Parametric function", 1.5, 0.0, 0.0, True, False, True,
                   False, False, True)
print "New parametric function item...",
if iface.newParFunItem(prefix + "fkt.so", "foursynth", prefix + "fkt.so",
                       "foursynth", 0.0, 6.28318, 0.0, 1, "ff", 1.0, 1.0, 0,
                       0.0, 0.0, 1.0, True):
  print "true"
else:
  print "false"
print "Changing parameters...",
for i in range(10):
  if iface.setPar(1, 2, i * 0.1, 1):
    print "true",
  else:
    print "false",
  if iface.setPar(1, 7, i * 0.1, 0):
    print "true",
  else:
    print "false",
sleep(3)
print "\nNew plot"
iface.newPlot()
print "New autoframe item"
iface.newAutoFrameItem()
print "New array item...",
if iface.newArrayItem(prefix + "demo7.dat", 0, 1, 2, 0, 51, True, 0, "0",
                      1.0, 1.0, 0, 0, 0.0, 0.0, 1.0, True):
  print "true"
else:
  print "false"
print "New array item...",
if iface.newArrayItem(prefix + "demo7.dat", 0, 3, 4, 0, 51, True, 0, "0",
                      1.0, 1.0, 0, 0, 0.0, 0.0, 1.0, True):
  print "true"
else:
  print "false"
print "Autoscaling...",
if iface.autoScaleFrame(0, 1):
  print "true"
else:
  print "false"
print "New function item...",
if iface.newFunItem(prefix + "fcompl.so", "epsilon_1", 2.0, 7.0, 0.0, 1, "0",
                    1.0, 1.0, 0, 0.0, 0.0, 1.0, False):
  print "true"
else:
  print "false"
print "Changing parameters...",
if iface.setPar(3, 0, 3.5, 1):
  print "true",
else:
  print "false",
if iface.setPar(3, 1, 8.5, 1):
  print "true",
else:
  print "false",
if iface.setPar(3, 2, 7500.0, 1):
  print "true",
else:
  print "false",
if iface.setPar(3, 3, 1.0, 1):
  print "true",
else:
  print "false",
if iface.setPar(3, 4, 150000.0, 1):
  print "true"
else:
  print "false"
print "Performing plot...",
if iface.activateItem(3, True):
  print "true"
else:
  print "false"
print "New function item...",
if iface.newFunItem(prefix + "fcompl.so", "epsilon_2", 2.0, 7.0, 0.0, 1, "ff",
                    1.0, 1.0, 0, 0.0, 0.0, 1.0, False):
  print "true"
else:
  print "false"
print "Changing parameters...",
if iface.setPar(4, 0, 3.5, 1):
  print "true",
else:
  print "false",
if iface.setPar(4, 1, 8.5, 1):
  print "true",
else:
  print "false",
if iface.setPar(4, 2, 7500.0, 1):
  print "true",
else:
  print "false",
if iface.setPar(4, 3, 1.0, 1):
  print "true",
else:
  print "false",
if iface.setPar(4, 4, 150000.0, 1):
  print "true"
else:
  print "false"
print "Performing plot...",
if iface.activateItem(4, True):
  print "true"
else:
  print "false"
print "Enabling fit parameters...",
for i in range(20):
  if i < 5:
    b = 1
  else:
    b = 0
  if iface.enableFitPar(i, b):
    print "true",
  else:
    print "false",
print "\nAdding fit items...",
if iface.addFitItems(True, 1, 3, True, "", ""):
  print "true",
else:
  print "false",
if iface.addFitItems(False, 2, 4, True, "", ""):
  print "true"
else:
  print "false"
sleep(3)
print "Performing fit...",
if  iface.fit(True, False, False):
  print "true"
else:
  print "false"
print "chi-square =",
print iface.chiSquare()
sleep(3)
print "New plot"
iface.newPlot()
print "New text item"
iface.newTextItem("#BNThe End", "cc6600", 1, 3.2, 7.5, 0.0, 5.0, True);
