I wanted to create a circle with this example code but it's not working with latest Elm version.
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (..)
main =
color brown canvas
canvas =
collage 200 200
[filled blue (circle 10)]
It looks like you are referencing an example from a previous version of Elm. An updated version of your code would look like this:
import Element exposing (..)
import Collage exposing (..)
import Color exposing (..)
main =
Element.toHtml (color brown canvas)
canvas =
collage 200 200
[filled blue (circle 10)]
This requires the evancz/elm-graphics package.
Related
I am trying to code a program based on traitsUI and Mayavi, but I have some problems. Following the code I am using:
#!/usr/bin/env python
import os
from traits.api import HasTraits, Instance, String, on_trait_change
from traitsui.api import View, Item
from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene
class ActorViewer(HasTraits):
scene = Instance(MlabSceneModel, ())
view = View(Item(name='scene',
editor=SceneEditor(scene_class=MayaviScene),
show_label=True,
resizable=True,
dock='tab',
height=500,
width=500),
resizable=True
)
def __init__(self, engine=None, **traits):
HasTraits.__init__(self, **traits)
if engine is not None:
self.scene=MlabSceneModel(engine=engine)
else:
self.scene=MlabSceneModel()
self.generate_data()
#on_trait_change('scene.activated')
def generate_data(self):
src=self.scene.mlab.pipeline.open(Path+i)
self.scene.mlab.view(40, 50)
self.scene.mlab.pipeline.outline(src)
self.scene.mlab.pipeline.iso_surface(src, contours=60, opacity=0.5)
if __name__ == '__main__':
Path = "/path/to/my/folder"
filelist = os.listdir(Path)
for i in filelist:
if i.endswith(".vtr"):
if ("E1_" in i) or ("E2_" in i):
print("file name ", i)
a = ActorViewer()
a.configure_traits()
The call self.scene.mlab.view(40, 50) returns AttributeError: 'NoneType' object has no attribute 'active_camera', thus I don't know how to set the camera. I have read that it is related to when the scene is activated, but I couldn't find a solution.
Without setting the view, the code works, but each file is loaded and rendered alone. In order to proceed with the main loop, each render has to be closed. I would like to dock each of the file without closing them.
I couldn't find a way to set a custom label to each tab after allowing show_label=True and to have it aligned horizontally at the top of the scene.
I tried to set the outline with the 'cornered' layout, but I couldn't find a way to do that. self.scene.mlab.pipeline.outline.outline_mode('cornered') gets simply ignored.
Thank you for your help!
I made a QWidget and inside I made some other items like QLabels which display images.
Consider what is inside that parent Widget I was trying to get the color where I would click.
Searching I found this thread but it is a bit old and I am not able to translate it to Python.
thread:
https://www.qtcentre.org/threads/49693-How-to-get-color-of-pixel-or-point
code:
QPixmap qPix = QPixmap::grabWidget(ui->myWidget);
QImage image(qPix.toImage());
QColor color(image.pixel(0, 1));
How would this translate this to PyQt5 if it is the correct answer?
QPixmap.grabWidget() is considered obsolete, and you should use QWidget.grab() instead.
pixmap = self.someWidget.grab()
img = pixmap.toImage()
color = img.pixelColor(0, 1)
I would like to use a Bokeh slider widget to show a list of month or a list of text instead of integer. E.g. Nov 2018, Dec 2018, Jan 2019, Feb 2019.....
Is it possible?
Many Thanks
As of Bokeh 1.3.4 Slider widget text is not configurable. Adding a hook for a formatter seems like a reasonable ask (and also a good task for a first-time contributor), so a Github Issue would be appropriate.
For now, you could put the slider in column with a Div and update the text property of the div with a CustomJS callback (or a Python callback, if this is a Bokeh server app).
Just to elaborate on what #bigreddot described:
from bokeh.models import CustomJS, Slider, Legend, Div
from bokeh.layouts import column
from bokeh.plotting import ColumnDataSource, show, output_file
div = Div(text= '<b>text0</b>', style={'font-size': '150%', 'color': 'blue'})
str_list = ['text0', 'text1', 'text2']
str_slider = Slider(start=0, end=len(str_list)-1, value=0, step=1, title="string")
callback = CustomJS(args=dict(div=div, str_list = str_list, str_slider=str_slider),
code="""
const v = str_slider.value
div.text = str_list[v]
""")
str_slider.js_on_change('value', callback)
layout = column(str_slider, div)
output_file('test.html')
show(layout)
I have a QGraphicsScene that contains multiple custom QGraphicsItems. Each item contains a QGraphicsProxyWidget which itself contains whatever widgets are needed by the business logic. The proxy has a Qt::Window flag applied to it, so that it has a title bar to move it around. This is all working well, except when moving a proxy widget when the view has been scaled.
The user can move around the scene à la google maps, ie by zooming out then zooming in back a little farther away. This is done with calls to QGraphicsView::scale. Items should always be visible no matter the zoom value, so they have the QGraphicsItem::ItemIgnoresTransformations flag set.
What happens when moving a proxyWidget while the view has been scaled is that on the first move event the widget will jump to some location before properly being dragged.
I had this issue with Qt5.7.1, and could reproduce it with PyQt5 as it is simpler to reproduce and hack around, please see the snippet below.
Steps to reproduce:
move the widget around, notice nothing unusual
use the mouse wheel to zoom in or out. The higher the absolute scale, the higher the effect on the issue.
click on the widget, and notice how it jumps on the first moving of the mouse.
Snippet:
import sys
import PyQt5
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsProxyWidget, QGraphicsWidget, QGraphicsObject
global view
global scaleLabel
def scaleScene(event):
delta = 1.0015**event.angleDelta().y()
view.scale(delta, delta)
scaleLabel.setPlainText("scale: %.2f"%view.transform().m11())
view.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
# create main widget
w = QWidget()
w.resize(800, 600)
layout = QVBoxLayout()
w.setLayout(layout)
w.setWindowTitle('Example')
w.show()
# rescale view on mouse wheel, notice how when view.transform().m11() is not 1,
# dragging the subwindow is not smooth on the first mouse move event
w.wheelEvent = scaleScene
# create scene and view
scene = QGraphicsScene()
scaleLabel = scene.addText("scale: 1")
view = QGraphicsView(scene)
layout.addWidget(view)
view.show();
# create item in which the proxy lives
item = QGraphicsWidget()
scene.addItem(item)
item.setFlag(PyQt5.QtWidgets.QGraphicsItem.ItemIgnoresTransformations)
item.setAcceptHoverEvents(True)
# create proxy with window and dummy content
proxy = QGraphicsProxyWidget(item, Qt.Window)
button = QPushButton('dummy')
proxy.setWidget(button)
# start app
sys.exit(app.exec_())
The jump distance is:
proportional to the scaling of the view , and to the distance of the mouse from the scene origin
goes from scene position (0,0) towards the mouse position (I think)
might be caused by the proxy widget not reporting the mouse press/move properly. I'm hinted at this diagnostic after looking at QGraphicsProxyWidgetPrivate::mapToReceiver in qgraphicsproxywidget.cpp (sample source), which does not seem to take scene scaling into account.
I am looking for either
confirmation that this is an issue with Qt and I did not misconfigured the proxy.
an explanation on how fix the mouse location given by the proxy to its children widgets (after installing a eventFilter)
any other workaround
Thanks
Almost 2 years later I got back to this issue again, and finally found a solution. Or rather a workaround, but a simple one at least. It turns out I can easily avoid getting into the issue with local/scene/ignored transforms in the first place.
Instead of parenting the QGraphicsProxyWidget to a QGraphicsWidget, and explicitly setting the QWidget as proxy target, I get the proxy directly from the QGraphicsScene, letting it set the window flag on the wrapper, and set the ItemIgnoresTransformations flag on the proxy. Then (and here's the workaround) I install an event filter on the proxy, intercept the GraphicsSceneMouseMove event where I force the proxy position to currentPos+mouseDelta (both in scene coordinates).
Here's the code sample from above, patched with that solution:
import sys
import PyQt5
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
global view
global scaleLabel
def scaleScene(event):
delta = 1.0015**event.angleDelta().y()
view.scale(delta, delta)
scaleLabel.setPlainText("scale: %.2f"%view.transform().m11())
view.update()
class ItemFilter(PyQt5.QtWidgets.QGraphicsItem):
def __init__(self, target):
super(ItemFilter, self).__init__()
self.target = target
def boundingRect(self):
return self.target.boundingRect()
def paint(self, *args, **kwargs):
pass
def sceneEventFilter(self, watched, event):
if watched != self.target:
return False
if event.type() == PyQt5.QtCore.QEvent.GraphicsSceneMouseMove:
self.target.setPos(self.target.pos()+event.scenePos()-event.lastScenePos())
event.setAccepted(True)
return True
return super(ItemFilter, self).sceneEventFilter(watched, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
# create main widget
w = QWidget()
w.resize(800, 600)
layout = QVBoxLayout()
w.setLayout(layout)
w.setWindowTitle('Example')
w.show()
# rescale view on mouse wheel, notice how when view.transform().m11() is not 1,
# dragging the subwindow is not smooth on the first mouse move event
w.wheelEvent = scaleScene
# create scene and view
scene = QGraphicsScene()
scaleLabel = scene.addText("scale: 1")
view = QGraphicsView(scene)
layout.addWidget(view)
view.show();
button = QPushButton('dummy')
proxy = scene.addWidget(button, Qt.Window)
proxy.setFlag(PyQt5.QtWidgets.QGraphicsItem.ItemIgnoresTransformations)
itemFilter = ItemFilter(proxy)
scene.addItem(itemFilter)
proxy.installSceneEventFilter(itemFilter)
# start app
sys.exit(app.exec_())
Hoping this may help someone who's ended up in the same dead end I was :)
In Selenium WebDriver APIs, there is a method getLocation() which returns point, containing location of top left hand corner of the element. Let's say, it returns (x, y).
Is the point (x, y) with respective to Desktop's(browser's when it's in full screen mode) screen resolution or viewport's(The viewport, the visible area of the webpage within the browser window) resolution?
EDIT
Based on my answer to this question, I can guess getLocation() returns point with respective desktop's screen resolution or entire browser's(including toolbar, searchbar etc.) rather than viewport's. I just want to make sure that if my thinking is correct and if yes, why getLocation()'s behaviour is made like that? Is there any Selenium WebDriver method, which will return me point of element with respective to viewport?
Please correct if I'm wrong.
The "exact place" on the page regardless of visibility.
This is why when doing image cropping if the element doesn't match the whole screen it will try to crop outside the screenshot as the actual element size/position extends beyond the visible screen.
However, if you are looking for the actual page properties and comparing this to the element size based on the location you can determine if a scrollbar should be present or not. element in the example below is an implementation of IWebElement as well as ICoordinates as the single IWebElement would not provide you with the ICoordinates interface as utilized below.
Point p = element.LocationOnScreenOnceScrolledIntoView;
OpenQA.Selenium.Interactions.Internal.ICoordinates p2 = element.Coordinates;
The above two offer a slight difference in capturing the location. The LocationOnScreenOnceScrolledIntoView one includes scrolling to the element and then would return the location based on the top left corner of the screen.
The Coordinates gives the specific location in various ways and includes the LocationInViewport property which would provide what you are looking for I believe.
p = p2.LocationInViewport;
p = p2.LocationOnScreen;
p = p2.LocationInDom;
object obj = p2.AuxiliaryLocator;
In my understanding, it returns the point with respective Desktop's screen resolution or entire browser's(including toolbar, searchbar etc.), not viewpoint's.
Here is what I tried. In following code, I'm getting source element's location(which is point (x,y) of top left corner of the element) And moving mouse to that point. If you see, the mouse pointer isn't moved to the element's point rather it's moved little above of the element. This makes me to guess that getLocation() returns point of element with respective to desktop's screen resolution. Please carefully observe the mouse pointer before and after
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class DragAndDropUsingRobot {
WebDriver driver = null;
#BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
}
#AfterClass
public void tearDown(){
driver.quit();
}
#Test
public void testDragAndDrop() throws AWTException, InterruptedException{
WebElement source = driver.findElement(By.id("div1"));
Robot robot = new Robot();
Thread.sleep(2000);
robot.mouseMove(source.getLocation().getX(), source.getLocation().getY());
Thread.sleep(5000);
}
}
Location (in python) refers to the location of the top left corner of an element area in the current renderable canvas. If you add the element's height and width to that, you get the bottom right corner of the element.
Also I believe the value is scaled - at least in Chrome's case, because it can contain float values, e.g.:
'location': {'y': 6825, 'x': 519.5}, 'size': {'width': 476, 'height': 16}
It can't be the desktop position, because it can return values larger than the desktop resolution. If you wish to have a desktop position, location_once_scrolled_into_view is probably at least pretty close, but I haven't worked with it yet: It tells us
where on the screen an element is so that we can click it. This method
should cause the element to be scrolled into view.
At least in python, it's an element property and not a method, and doesn't scroll an element into view AFAIK.