Button appears on hover - raphael

I have a box. when you put your mouse over the box, a button appears on top of the box. the hover function works in such a way that it doesn't recognize that the mouse is still on top of the box. how can I solve it?
//I create the paper
var paper = Raphael(0, 0, 500,500);
//I add the box
var box = paper.add([{
type: "rect",
x: 100,
y: 100,
width: 100,
height: 100,
fill: '#000',
}])
// I declare a varible for the button
var button
//I add hover functions to the box.
//first function: when the mouse is on, create a red circle and add an
//onclick event to the circle
box.hover(function () {
button = paper.circle(150, 150, 25).attr({ 'fill': 'red' })
button.click(function () { alert("You clicked me!")})
},
//second function: when the mouse leaves the box, remove the circle
function () {
button.remove()
})
Here is an example
http://jsfiddle.net/V4E4Q/

You will have to deal with events from different objects one way or another, either I guess with a couple of handlers, or with one that has event propagation and handling possibly, or group with a set. The current event is coming from the button when its displayed, so the event logic doesn't quite make sense as it is. So you could add a handler to the button as well (edit, or use a set for example).
One example would be...
var button = paper.circle(150, 150, 25).attr({ 'fill': 'red' });
button.hide();
button.click( function() { alert("button clicked"); } );
box.mouseout( function() { button.hide() });
button.mouseover( function() { button.show(); });
box.mouseover(function () { button.show(); } );
I'm not sure if this is the most efficient way, but it should work. jsfiddle here http://jsfiddle.net/V4E4Q/1/
Edit: Another possibly cleaner way could be to group the elements into a set and handle the click. Here is a modified example http://jsfiddle.net/V4E4Q/3/

Related

Ember: Handling window resize within a component

I have a component which renders a selection of images, and I have some CSS for a "hover mask" which then covers the image, displaying some information about that image when a user hovers over it.
The dimensions of the hover mask need to match those of the image when the app first loads, and when the window resizes.
Here's the basic mark up
<div class="call-out-item">
<img src="......">
<div class="hover-mask">Some info about this image</div>
</div>
Here's the CSS:
.call-out-item {
position:relative;
display:none;
}
.call-out-item .hover-mask {
position:absolute;
top:0;
left:0;
}
.call-out-item:hover .hover-mask {
opacity: .95;
background: rgba(33,153,232,0.8);
cursor:pointer;
}
Here's my component code (not sure about the correct use of Ember.run.next (!)
// other component code here
const {$} = Ember;
init() {
this._super(...arguments);
this.handleResize = function() {
let itemWidth = $(".call-out-item img").width();
let itemHeight = parseInt(itemWidth/1.5);
$(".hover-mask").css({"width":itemWidth,"height":itemHeight});
}.bind(this);
Ember.run.next(this, this.handleResize);
$(window).on('resize', Ember.run.bind(this, this.handleResize));
},
// etc etc
I am getting variable results with this code in Chrome and Firefox so must be doing something wrong here!
Make sure you unbind the event when the component is destroyed, otherwise your event will continue to trigger while the user is on other routes
initResizeObserver: on("init", function () {
$(window).on('resize', this.handleResize.bind(this));
}),
willDestroyElement () {
this._super(...arguments);
$(window).off("resize");
},
handleResize (e) {
console.log(e)
},
Here is my solution. One of the problems (in Firefox) was that the above code was not calculating the width of the image initially, so I am now calculating the width (and height) of the parent container and setting the dimensions of the image to 100% for width and height.
I still think there are some glitches when I resize the window (does anyone know why?) but I hope this helps!
// component code
import Ember from 'ember';
const {$} = Ember;
export default Ember.Component.extend({
tagName:'div',
classNames:[],
init() {
this._super(...arguments);
this.handleResize = function() {
let itemWidth = parseInt($(".call-out-item").width());
let itemHeight = parseInt(itemWidth/1.5);
$(".hover-mask").css({"width":itemWidth,"height":itemHeight});
};
Ember.run.next(this, this.handleResize);
$(window).on('resize', Ember.run.bind(this, this.handleResize));
},
// rest of code

How to switch Qt5 Controls QML styles in realtime

So my objective is to set up themes for an application that I have made. I would like to be able to check a menu option that will change the theme of the application as soon as I press it; and change the theme back to the native one on pressing the button. I understand how to style the Qt Quick Controls but I am having trouble with the loading and unloading of these themes. Currently my application comes with the native-like theme that Qt Controls comes with. I am able to load custom styles but I am unable to swap them the custom styles and the native theme. How would I go about creating this feature?
For example I am trying to swap the theme of a TableView. I first tried setting a property of TableViewStyle to be styleData.value This produced the desired effect of keeping the native-like theme, but now how would I go about assigning the Rectangle QmlObject to a property value? The interpreter complains about how I cannot assign a Object to a property.
TableViewStyle {
property string color: styleData.value
headerDelete: color // This does not work. Because
// styleData.value in undefined
//property string color: rect // Currently doing this does not
//Rectangle {id: rect; color: "red"} // have any effect on the value of
//headerDelegate: color // the headerDelegate. Putting the
// Rectangle in a Component produces
// The same value too.
}
I did not find a way to go back to the default style without having to load the element first with the default style and save it but this works for a Button switching between two custom styles and the default ButtonStyle
// Property used to save the default ButtonStyle
property var defaultStyle: undefined
// Property used to know the current style
property string currentStyle: "blue"
Button {
id: myBtn
text: qsTr("Hello World")
anchors.centerIn: parent
onClicked: {
if(currentStyle == "blue") {
style = myButtonStyleRed;
currentStyle = "red";
} else if(currentStyle == "red") {
style = defaultStyle;
currentStyle = "default";
} else {
style = myButtonStyleBlue;
currentStyle = "blue";
}
}
// Save the default Button style on load before switching to the blue one
Component.onCompleted: {
defaultStyle = myBtn.style;
myBtn.style = myButtonStyleBlue;
}
}
Component {
id: myButtonStyleBlue
ButtonStyle {
label: Text {
color: "blue"
text: control.text
}
}
}
Component {
id: myButtonStyleRed
ButtonStyle {
label: Text {
color: "red"
text: control.text
}
}
}

Simulate clicked QML element from QTest

I'm playing around with QTest and want simulate a mouse click in my test on one of my QML elements that I have in my UI.
So in other words I would like to trigger the onClicked signal handler in my QML code from C++?
This is how my QML looks like:
import QtQuick 2.0
Rectangle
{
objectName: "MessageRectangle"
width: 360
height: 360
Text
{
objectName: "MessageText"
text: qsTr("Hello World")
anchors.centerIn: parent
}
Rectangle
{
id: simplebutton
objectName: "ChangeTextButton"
color: "grey"
width: 150; height: 75
Text
{
id: buttonLabel
anchors.centerIn: parent
text: "button label"
}
MouseArea
{
id: buttonMouseArea
objectName: "ChangeTextButtonMouseArea"
anchors.fill: parent
onClicked:
{
CppApi.setMessage( "Button Clicked" );
}
}
}
}
It is a bit hard to show all the different solutions that I have tried. But I have tried to connect an own class derived from QObject with the QML item and sending click signal to it:
QObject::connect( this, SIGNAL( clicked() ), pItem, SLOT( onClicked() ) );
emit clicked();
I have also tried to call the following way:
QMetaObject::invokeMethod( pButton, "pressedChanged", Qt::DirectConnection );
Where pButton is the QQuickItem which I get when I grab the object named ChangeTextButtonMouseArea.
I have found some similar questions here. But there seems to be something different with the onClicked, somehow. If I print out all the methods that are defined in the QMetaObject that I get from the QQuickItem, then I can't find the onClicked function or the clicked signal.
This answer is somewhat in the correct direction but the the signal I want to send is not created by myself:
C++ SIGNAL to QML SLOT in Qt
I also found this question:
How to simulate mouse clicks in QML?
But I only want to trigger a click event on a specific QML object. Do I really need to calculate the coordinates of my mouse click? Doing that seems a bit complicated since the QML items are only aware of the relative positions to their parents.
Am I trying something that is not possible? Or is the reason that I can't find any answers on the internet because the solution is so obvious?
Found out how to simulate the mouse click of an item. Posting it here in case someone else is trying to do this. It is actually quite simple. Use compute the coordinate to click and use QTest do the actual clicking.
void ClickItem( QQuickItem* pItem, QWindow* pRootWindow )
{
auto oPointF = pItem->mapToScene( QPoint( 0, 0 ) );
auto oPoint = oPointF.toPoint();
oPoint.rx() += pItem->width() / 2;
oPoint.ry() += pItem->height() / 2;
QTest::mouseClick( pRootWindow, Qt::LeftButton, Qt::NoModifier, oPoint );
}

Mootools Morph Height and back

Im looking for a solution for:
I want to toggel the height of a Div but not complet.
So i try this but didnt work. I see the first classchange in firebug and the heigth change to 36px.
But back didnt work. I try wiht add and remove class.
http://jsfiddle.net/Zbbd9/1/
$$('.outer').setStyle('height', '660px');
$$('.outer h2').addEvent('click', function(){
if (this.hasClass('close')) {
var parent = this.getParent();
parent.removeClass('close');
parent.morph({
'height': '660px'
});
} else {
var parent = this.getParent();
parent.addClass('close');
parent.morph({
'height': '36px'
});
}
});
UPDATE:
http://jsfiddle.net/Zbbd9/2/
Got it.
Best Regards
You're selecting the wrong parent container.
Try it like this:
$$('.outer h2').addEvent('click', function(){
var parent = this.getParent();
if (parent.hasClass('close')) {
parent.removeClass('close');
parent.morph({
'height': '660px'
});
} else {
parent.addClass('close');
parent.morph({
'height': '36px'
});
}
});
Adding a class to identify the current state is a solid way to do it, but you might want to consider other alternatives, too, like using toggleClass or referring to something completely different like a slider or similar.

JQuery Dialog Close Event

$("#termSheetPrinted").dialog({
autoOpen: false,
resizable: true,
height: $(window).height() - 50,
width: $(window).width() - 50,
position: 'center',
title: 'Term Sheet',
beforeClose: function(event, ui) { $("#termSheetPrinted").html(''); },
modal: true,
buttons: {
"Print": function () {
$("#termSheetPrinted").jqprint();
},
"Cancel": function () {
$("#termSheetPrinted").html('');
$(this).dialog("close");
}
}
});
So, when I click 'Cancel', I can generate the dialog right over again and everything looks fine. If I click the 'X' in the upper right corner and generate it again, it doubles up, having not been cleared from the last time.
I tried adding the beforeClose event to clear the HTML, however it doesn't seem to be working.
How can I get it to clear and close properly from both 'Cancel', and hitting the 'X'?
Seems to work if I bind it to close instead.
Shouldn't this work both ways though?