3 changes: 3 additions & 0 deletions pyKst/demo/datavector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
c1 = client.new_curve(V1, V2)
p1 = client.new_plot(font_size = 12)
p1.add(c1)

V1.change_frames(1000, 500, 0, False)
V2.change_frames(1000, 500, 0, False)
4 changes: 2 additions & 2 deletions pyKst/demo/plot_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
client.new_tab()
client.set_tab_text("Third")

for i in xrange(1,10):
for i in xrange(1,26):
p = client.new_plot(font_size = 8, auto_position = False)
p.subplot(3,3,i)
p.subplot(5,5,i)

client.show_window()

7 changes: 6 additions & 1 deletion pyKst/demo/viewitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@

Label2 = client.new_label("Sub Label Here", (0.25,0.25), 0, 16, font_family="courier")
Label2.set_font_bold(True)
Label2.update_parent()
Label2.set_parent_auto()

P1.set_pos((0.5,0.5))

print Label2.position(), Label2.dimensions()

Label2.set_parent_toplevel()
52 changes: 51 additions & 1 deletion pyKst/pykst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import atexit
import os

from ast import literal_eval

try:
from PySide import QtCore, QtNetwork, QtGui
except ImportError as err1:
Expand Down Expand Up @@ -1105,6 +1107,22 @@ def change(self, filename, field, start, num_frames, skip, boxcarFirst):
+b2str(start)+","+b2str(num_frames)+","+b2str(skip)
+","+b2str(boxcarFirst)+")")

def change_frames(self, start, num_frames, skip, boxcarFirst):
""" Change the parameters of a data vector.
:param start: The starting index of the vector.
start = -1 for count from end.
:param num_frames: The number of frames to read.
num_frames = -1 for read to end.
:param skip: The number of frames per sample read.
skip = 0 to read every sample.
:param boxcarFirst: apply a boxcar filter before skiping.
"""
self.client.send_si(self.handle, "changeFrames("
+b2str(start)+","+b2str(num_frames)+","+b2str(skip)
+","+b2str(boxcarFirst)+")")

def field(self):
""" Returns the fieldname. """
return self.client.send_si(self.handle, "field()")
Expand Down Expand Up @@ -2328,6 +2346,20 @@ def set_fixed_aspect_ratio(self, fixed=True):
else:
self.client.send_si(self.handle, b2str("uncheckFixAspectRatio()"))

def position(self):
x = str(self.client.send_si(self.handle, "position()"))

ret=literal_eval(x)

return ret

def dimensions(self):
x = str(self.client.send_si(self.handle, "dimensions()"))

ret=literal_eval(x)

return ret

def set_pos(self,pos):
""" Set the center position of the item.
Expand Down Expand Up @@ -2362,9 +2394,27 @@ def set_size(self,size):
self.client.send("setGeoY("+b2str(h)+")")
self.client.send("endEdit()")

def update_parent(self):
def set_parent_auto(self):
"""
Set the parent of the viewitem to an existing view item which fully contains it.
Once reparented, moving/resizing the parent will also move/resize the child.
By default view items created by pyKst are parented by the toplevel view unless
this method is called, or if the item is moved/resized in the GUI.
"""

self.client.send_si(self.handle, "updateParent()")

def set_parent_toplevel(self):
"""
Set the parent of the viewitem to the toplevel view.
By default view items created by pyKst are parented by the toplevel view unless
set_parent_auto() is called, or if the item is moved/resized in the GUI.
"""

self.client.send_si(self.handle, "parentTopLevel()")

def subplot(self, *args):
"""
Set the item position according to the given grid definition.
Expand Down
1 change: 1 addition & 0 deletions src/libkst/datavector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ void DataVector::changeFile(DataSourcePtr in_file) {
void DataVector::changeFrames(int in_f0, int in_n,
int in_skip, bool in_DoSkip,
bool in_DoAve) {

Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED);

if (dataSource()) {
Expand Down
17 changes: 17 additions & 0 deletions src/libkst/vectorscriptinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ DataVectorSI::DataVectorSI(DataVectorPtr it) {
}

_fnMap.insert("change",&DataVectorSI::change);
_fnMap.insert("changeFrames",&DataVectorSI::changeFrames);
_fnMap.insert("field",&DataVectorSI::field);
_fnMap.insert("filename",&DataVectorSI::filename);
_fnMap.insert("start",&DataVectorSI::start);
Expand Down Expand Up @@ -191,6 +192,22 @@ QByteArray DataVectorSI::endEditUpdate() {
/* data vector commands */
/***************************/

QString DataVectorSI::changeFrames(QString &command) {
QStringList vars = getArgs(command);

_datavector->writeLock();
_datavector->changeFrames(
vars.at(0).toInt(), // f0
vars.at(1).toInt(), // n
vars.at(2).toInt(), // skip
vars.at(2).toInt() > 0, // do skip
vars.at(3) == "True" // do average
);
_datavector->unlock();
return "Done";
}


QString DataVectorSI::change(QString& command) {
QStringList vars = getArgs(command);

Expand Down
1 change: 1 addition & 0 deletions src/libkst/vectorscriptinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class KSTCORE_EXPORT DataVectorSI : public VectorCommonSI
static ScriptInterface* newVector(ObjectStore *store);

QString change(QString &command);
QString changeFrames(QString &command);
QString field(QString &command);
QString filename(QString &command);
QString start(QString &command);
Expand Down
3 changes: 2 additions & 1 deletion src/libkstapp/plotrenderitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,8 @@ QPainterPath PlotRenderItem::shape() const {
}


bool PlotRenderItem::updateViewItemParent() {
bool PlotRenderItem::updateViewItemParent(bool force_toplevel) {
Q_UNUSED(force_toplevel)
return false; //never reparent a plot renderer
}

Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/plotrenderitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class PlotRenderItem : public ViewItem
void dragXZoomMouseCursor(qreal x);
QPointF statusMessagePoint;

virtual bool updateViewItemParent();
virtual bool updateViewItemParent(bool force_toplevel = false);

public Q_SLOTS:
virtual void edit();
Expand Down
61 changes: 31 additions & 30 deletions src/libkstapp/viewitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ QPointF ViewItem::lockOffset(const QPointF &offset, qreal ratio, bool oddCorner)
}


bool ViewItem::updateViewItemParent() {
bool ViewItem::updateViewItemParent(bool force_toplevel) {
if (lockParent() || skipNextParentCheck()) {
setSkipNextParentCheck(false);
return false;
Expand Down Expand Up @@ -1870,56 +1870,57 @@ bool ViewItem::updateViewItemParent() {
return true;
}

//Look for collisions that completely contain us
foreach (QGraphicsItem *item, collisions) {
ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
if (!force_toplevel) {
//Look for collisions that completely contain us
foreach (QGraphicsItem *item, collisions) {
ViewItem *viewItem = dynamic_cast<ViewItem*>(item);

if (!viewItem || !viewItem->acceptsChildItems() || isAncestorOf(viewItem) || !collidesWithItem(viewItem, Qt::ContainsItemBoundingRect)) {
if (!viewItem || !viewItem->acceptsChildItems() || isAncestorOf(viewItem) || !collidesWithItem(viewItem, Qt::ContainsItemBoundingRect)) {
#if DEBUG_REPARENT
qDebug() << "rejecting collision" << viewItem << !viewItem->acceptsChildItems() <<
isAncestorOf(viewItem) << !collidesWithItem(viewItem, Qt::ContainsItemBoundingRect);
qDebug() << "rejecting collision" << viewItem << !viewItem->acceptsChildItems() <<
isAncestorOf(viewItem) << !collidesWithItem(viewItem, Qt::ContainsItemBoundingRect);
#endif
continue;
}
continue;
}

if (parentItem() == viewItem) { /*already done*/
if (parentItem() == viewItem) { /*already done*/
#if DEBUG_REPARENT
qDebug() << "already in containing parent";
qDebug() << "already in containing parent";
#endif
return false;
}
return false;
}

#if DEBUG_REPARENT
qDebug() << "reparent to" << viewItem;
qDebug() << "reparent to" << viewItem;

qDebug() << "before transform"
<< "origin:" << mapToScene(QPointF(0,0));
qDebug() << "before transform"
<< "origin:" << mapToScene(QPointF(0,0));
#endif

if (!topLevel) { /*bring the old parent's transform with us*/
setTransform(parentItem()->transform(), true);
}
if (!topLevel) { /*bring the old parent's transform with us*/
setTransform(parentItem()->transform(), true);
}

/*cancel out the new parent's initial transform*/
setTransform(viewItem->transform().inverted(), true);
/*cancel out the new parent's initial transform*/
setTransform(viewItem->transform().inverted(), true);

#if DEBUG_REPARENT
qDebug() << "after transform"
<< "origin:" << mapToScene(QPointF(0,0));
qDebug() << "after transform"
<< "origin:" << mapToScene(QPointF(0,0));
#endif

setParentViewItem(viewItem);
setPos(mapToParent(mapFromScene(origin)) + pos() - mapToParent(QPointF(0,0)));
updateRelativeSize(true);
setParentViewItem(viewItem);
setPos(mapToParent(mapFromScene(origin)) + pos() - mapToParent(QPointF(0,0)));
updateRelativeSize(true);

#if DEBUG_REPARENT
qDebug() << "after new parent"
<< "origin:" << mapToScene(QPointF(0,0));
qDebug() << "after new parent"
<< "origin:" << mapToScene(QPointF(0,0));
#endif

return true;
return true;
}
}

//No suitable collisions then reparent to top-level
if (!topLevel) {
#if DEBUG_REPARENT
Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/viewitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class ViewItem : public QObject, public NamedObject, public QGraphicsRectItem

ScriptInterface *scriptInterface();

virtual bool updateViewItemParent();
virtual bool updateViewItemParent(bool force_toplevel = false);

Q_SIGNALS:
void geometryChanged();
Expand Down
29 changes: 27 additions & 2 deletions src/libkstapp/viewitemscriptinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,35 @@ QString DimensionTabSI::doCommand(QString x) {
return "done";
}

if (command == "parentTopLevel") {
item->updateViewItemParent(true);
return "done";
}

if(command=="fixAspectRatioIsChecked") {
return item->lockAspectRatio()?"true":"false";
}

if (command == "position") {
QString retval;
if(item->dataPosLockable() && item->lockPosToData()) {
retval = QString("(%1, %2)").arg(item->dataRelativeRect().center().x()).arg(item->dataRelativeRect().center().y());
} else {
retval = QString("(%1, %2)").arg(item->relativeCenter().x()).arg(item->relativeCenter().y());
}
return retval;
}

if (command == "dimensions") {
QString retval;
if(item->dataPosLockable() && item->lockPosToData()) {
retval = QString("(%1, %2)").arg(item->dataRelativeRect().width()).arg(item->dataRelativeRect().height());
} else {
retval = QString("(%1, %2)").arg(item->relativeWidth()).arg(item->relativeHeight());
}
return retval;
}

if(!command.startsWith("setGeo")&&!command.startsWith("setPos")&&
!command.contains("checkFixAspect")&&!command.contains("setRotation")) {
return "";
Expand Down Expand Up @@ -298,7 +323,7 @@ ScriptInterface* ViewItemSI::newPicture(QByteArray picf) {
kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
bi->setViewRect(0.9,0.9,1.0,1.0,true);
bi->setVisible(1);
bi->updateViewItemParent();
//bi->updateViewItemParent();
return new ViewItemSI(bi);
}

Expand All @@ -308,7 +333,7 @@ ScriptInterface* ViewItemSI::newSvgItem(QByteArray path) {
kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
bi->setViewRect(0.9,0.9,1.0,1.0,true);
bi->setVisible(1);
bi->updateViewItemParent();
//bi->updateViewItemParent();
return new ViewItemSI(bi);
}
#endif
Expand Down