Reply
Thread Tools
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#1
Hello,

I am modifying EasyPaint to practice on different aspects of Qt, and I am trying to add a zoom slider function onto the status bar, I got it to work but there is a bug that I can't figure out.. Basically, the zoom doesn't work properly.. If someone can help me with this problem I will definitely appreciate it! <3

This is the code that I've added to mainwindow.h (in 'private slots'):

Code:
void zoomSlider(int level);
And this is what I've added to mainwindow.cpp (in "void MainWindow::initializeStatusBar()")

Code:
	QSlider *zoomSliderBar = new QSlider(Qt::Horizontal, this);
	
	zoomSliderBar->setTickPosition(QSlider::TicksBothSides);
	// Slider not inverted
	zoomSliderBar->setInvertedAppearance(false);
	// Min value to max value
	zoomSliderBar->setRange(0,4);
	// Initial Value of Slider Bar
	zoomSliderBar->setValue(2);
	// Add the widget at the bottom (status bar)
	mStatusBar->addPermanentWidget(zoomSliderBar, 1);

	connect(zoomSliderBar,SIGNAL(valueChanged(int)), this, SLOT(zoomSlider(int)));
And finally I also added this to the mainwindow.cpp (on top of the "initializeStatusBar()" function)

Code:
void MainWindow::zoomSlider(int level) // Zoom Slider Bar Function
{
	if (level == 0)
	{
		getCurrentImageArea()->zoomImage(0.25);
		getCurrentImageArea()->setZoomFactor(0.25);
	}

	else if (level == 1)
	{
		getCurrentImageArea()->zoomImage(0.50);
		getCurrentImageArea()->setZoomFactor(0.50);
	}

	else if (level == 2)
	{
		getCurrentImageArea()->zoomImage(1);
		getCurrentImageArea()->setZoomFactor(1);
	}

	else if (level == 3)
	{
		getCurrentImageArea()->zoomImage(1.50);
		getCurrentImageArea()->setZoomFactor(1.50);
	}

	else if (level == 4)
	{
		getCurrentImageArea()->zoomImage(2);
		getCurrentImageArea()->setZoomFactor(2);
	}
}
I personally think maybe my "void MainWindow::zoomSlider(int level)" is not working properly, but I don't know how to fix it..
__________________
FarahFa.com
 
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#2
What I mean by zoom not working properly, it zooms okay the first time you use the slider bar, then it just goes crazy everytime I try to use the slider (as in it zooms in and out but not in the order that it suppose to)...
__________________
FarahFa.com
 
pichlo's Avatar
Posts: 6,445 | Thanked: 20,981 times | Joined on Sep 2012 @ UK
#3
I would start by adding a qDebug in your zoomSlider method to see the actual values. Another thing I would do is replace the if-else ladder with a switch or, better still, with something like zoom(level * 0.5).
 

The Following User Says Thank You to pichlo For This Useful Post:
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#4
Hello pichlo, thank you for replying back to me.. I'm not going to lie to you, I'm a beginner when it comes to Qt, so please excuse my noobness, but I tried using the qDebug but I am not even sure if I'm doing that correctly either (even when I read about it a bit online).. and when you say zoom(level * 0.5), what do you mean by that?

Did you mean something like this?

Code:
void MainWindow::zoomSlider(int level) // Zoom Slider Bar Function
{
	switch (level)
	{
	case 0:
		{
			getCurrentImageArea()->zoomImage(level * 0.50);
			getCurrentImageArea()->setZoomFactor(level * 0.50);
		}
	case 1:
		{
			getCurrentImageArea()->zoomImage(level * 1);
			getCurrentImageArea()->setZoomFactor(level * 1);
		}
	case 2:
		{
			getCurrentImageArea()->zoomImage(level* 1.50);
			getCurrentImageArea()->setZoomFactor(level * 1.50);
		}
	case 3:
		{
			getCurrentImageArea()->zoomImage(level* 2);
			getCurrentImageArea()->setZoomFactor(level * 2);
		}
	}
}
EDIT: Lol, now it just zooms in no matter what.. xD

I noticed that also clicking on a certain level rather than sliding the bar it directly either goes to the min or the max..
__________________
FarahFa.com

Last edited by bandora; 2014-10-21 at 19:37.
 
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#5
Okay, so it kind of works now, but not exactly how it should either..

Here's my code for it so far:

Code:
void MainWindow::zoomSlider(int level) // Zoom Slider Bar Function
{
	switch (level)
	{
	case 0:
		{
			getCurrentImageArea()->zoomImage(0.50);
			getCurrentImageArea()->setZoomFactor(0.50);
			break;
		}
	case 1:
		{
			getCurrentImageArea()->zoomImage(1);
			getCurrentImageArea()->setZoomFactor(1);
			break;
		}
	case 2:
		{
			getCurrentImageArea()->zoomImage(2);
			getCurrentImageArea()->setZoomFactor(2);
			break;
		}
	case 3:
		{
			getCurrentImageArea()->zoomImage(4);
			getCurrentImageArea()->setZoomFactor(4);
			break;
		}
	}
}
Basically, it doesn't have set zoom to it.. Hard to explain it but, like if you go to the min value it's not like it's set to make it only 50% for example, but it does zoom out, but it's setting it to a specific value..
__________________
FarahFa.com

Last edited by bandora; 2014-10-21 at 19:40.
 
pichlo's Avatar
Posts: 6,445 | Thanked: 20,981 times | Joined on Sep 2012 @ UK
#6
Originally Posted by bandora View Post
I'm not going to lie to you, I'm a beginner when it comes to Qt, so please excuse my noobness
Hey, no worries, I am a learner myself!

I tried using the qDebug but I am not even sure if I'm doing that correctly either (even when I read about it a bit online)..
Just use it as printf, it's as simple as that. I use it all the time when developing for the N900, mostly because I do virtually all my development on the device itself and have limited scope for other forms of debugging.

You will only see the qDebug output if you launch your program from the command line, I may have forgotten to mention tht little fact.

and when you say zoom(level * 0.5), what do you mean by that?
I mean, in your first example, you have 5 cases of mapping the parameter 'level' to some different number:
0 -> 0.25
1 -> 0.5
2 -> 1.0
3 -> 1.5
4 -> 2.0
The first thing that stands out is that with the exception of the first case, the mapped number is always 0.5 times the value of the parameter 'level'. In other words, level*0.5.

Here is what I meant in code. Either...

Code:
void MainWindow::zoomSlider(int level) // Zoom Slider Bar Function
{
	qDebug("zoomSlider() called, level==%d", level);

	switch (level)
	{
	case 0:
		getCurrentImageArea()->zoomImage(0.25);
		getCurrentImageArea()->setZoomFactor(0.25);
		break;
	case 1:
		getCurrentImageArea()->zoomImage(0.5);
		getCurrentImageArea()->setZoomFactor(0.5);
		break;
	case 2:
		getCurrentImageArea()->zoomImage(1.0);
		getCurrentImageArea()->setZoomFactor(1.0);
		break;
	case 3:
		getCurrentImageArea()->zoomImage(1.5);
		getCurrentImageArea()->setZoomFactor(1.5);
		break;
	case 4:
		getCurrentImageArea()->zoomImage(2.0);
		getCurrentImageArea()->setZoomFactor(2.0);
		break;
	}
}
(You have some 'break' statements missing in your code, but I see you've fixed it.)

...or...

Code:
void MainWindow::zoomSlider(int level) // Zoom Slider Bar Function
{
	qDebug("zoomSlider() called, level==%d", level);

	if (level == 0)
	{
		getCurrentImageArea()->zoomImage(0.25);
		getCurrentImageArea()->setZoomFactor(0.25);
	}
	else
	{
		getCurrentImageArea()->zoomImage(level * 0.5);
		getCurrentImageArea()->setZoomFactor(level * 0.5);
	}
}
Originally Posted by bandora View Post
Basically, it doesn't have set zoom to it.. Hard to explain it but, like if you go to the min value it's not like it's set to make it only 50% for example, but it does zoom out, but it's setting it to a specific value..
I'm afraid you've lost me there. But now your zoomSlider method is more or less correct, have a look at the rest of your code.

Last edited by pichlo; 2014-10-21 at 20:01. Reason: Reacted to OP's additions posted while I was typing
 

The Following 2 Users Say Thank You to pichlo For This Useful Post:
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#7
Originally Posted by pichlo View Post
SNIP..

I'm afraid you've lost me there. But now your zoomSlider method is more or less correct, have a look at the rest of your code.
Yeah, I'm sorry I can't really explain it that well.. Thank you for your help so far!! The qDebug is running nicely!!

And it seems on every tick the slider uses the correct "level", but the image isn't being resized like how it's suppose to.. :/ I'm going to upload the exe so you can see what I'm talking about..

EDIT: Here it is: http://dl.farahfa.com/tmp/EasyPaint
__________________
FarahFa.com
 
pichlo's Avatar
Posts: 6,445 | Thanked: 20,981 times | Joined on Sep 2012 @ UK
#8
Sorry, I am a bit short on time but even if I were not, I am not going to run a random exe. No offence intended, just a personal policy.

However, to expand on this...

Originally Posted by pichlo View Post
have a look at the rest of your code.
...I would have a look at what's behind your getCurrentImageArea(), zoomImage() and setZoomFactor() and why are both zoomImage() and setZoomFactor() called each time. Maybe only one needs to be called?

Here is an excellent example of a simple image zooming code, straight from the creators of Qt:

http://qt-project.org/doc/qt-4.8/wid...ageviewer.html

It might be a good idea to have a read through that article. It is quite long and a bit fragmented (it never shows the whole code), so to fully comprehend it you may need to read it front to end. It may not fit your specifications exactly but it may give you some inspiration.
 

The Following User Says Thank You to pichlo For This Useful Post:
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#9
Originally Posted by pichlo View Post
Sorry, I am a bit short on time but even if I were not, I am not going to run a random exe. No offence intended, just a personal policy.

However, to expand on this...



...I would have a look at what's behind your getCurrentImageArea(), zoomImage() and setZoomFactor() and why are both zoomImage() and setZoomFactor() called each time. Maybe only one needs to be called?

Here is an excellent example of a simple image zooming code, straight from the creators of Qt:

http://qt-project.org/doc/qt-4.8/wid...ageviewer.html

It might be a good idea to have a read through that article. It is quite long and a bit fragmented (it never shows the whole code), so to fully comprehend it you may need to read it front to end. It may not fit your specifications exactly but it may give you some inspiration.
I understand, I would also be hesitant to try out a random exe from someone I don't know..

Either way, I really want to thank you for all your help! <3
__________________
FarahFa.com
 
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#10
I had a look the EasyPaint code, and the existing zoom implementations call both zoomImage() and setZoomFactor(), so there is no problem there (see https://github.com/Gr1N/EasyPaint/bl....cpp#L641-L662).

I think you are setting the zoom factor incorrectly. The zoom values are not meant to be absolute, but relative to the previous value. Therefore, in your implementation, the result will differ depending on where you start from. For example, changing the slider value from 4 to 1 will produce a different result than moving from 2 to 1, and moving from n to 2 will not change the zoom at all.

What you need to do is store the previous value of the slider, so when the value changes, you can calculate the correct zoom factor relative to the previous value:

Code:
void MainWindow::zoomSlider(int level) {
    const qreal zoomFactor = level > oldLevel ? 2.0 : 0.5;
    qreal zoom = 1.0;

    for (int i = 0; i < qAbs(level - oldLevel); i++) {
        zoom *= zoomFactor;
    }

    getCurrentImageArea()->zoomImage(zoom);
    getCurrentImageArea()->setZoomFactor(zoom);

    oldLevel = level;
}
Disclaimer: The above code is not tested, and could be completely wrong.
__________________
'Men of high position are allowed, by a special act of grace, to accomodate their reasoning to the answer they need. Logic is only required in those of lesser rank.' - J K Galbraith

My website

GitHub

Last edited by marxian; 2014-10-21 at 23:10. Reason: Mathematical error
 

The Following 2 Users Say Thank You to marxian For This Useful Post:
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 06:51.