capybara-webkit/src/FrameFocus.cpp

75 lines
1.7 KiB
C++
Raw Permalink Normal View History

#include "FrameFocus.h"
#include "SocketCommand.h"
#include "WebPage.h"
#include "WebPageManager.h"
2013-02-02 22:32:54 +00:00
#include "ErrorMessage.h"
FrameFocus::FrameFocus(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
}
void FrameFocus::start() {
switch(arguments().length()) {
2011-04-20 23:02:20 +00:00
case 1:
focusId(arguments()[0]);
2011-04-20 23:02:20 +00:00
break;
case 2:
focusIndex(arguments()[1].toInt());
2011-04-20 23:02:20 +00:00
break;
default:
focusParent();
}
}
void FrameFocus::findFrames() {
frames = page()->currentFrame()->childFrames();
}
void FrameFocus::focusIndex(int index) {
findFrames();
2011-04-20 23:02:20 +00:00
if (isFrameAtIndex(index)) {
frames[index]->setFocus();
page()->setCurrentFrameParent(frames[index]->parentFrame());
2011-04-20 23:02:20 +00:00
success();
} else {
2011-04-20 23:02:20 +00:00
frameNotFound();
}
}
bool FrameFocus::isFrameAtIndex(int index) {
return 0 <= index && index < frames.length();
}
void FrameFocus::focusId(QString name) {
findFrames();
2011-04-20 23:02:20 +00:00
for (int i = 0; i < frames.length(); i++) {
if (frames[i]->frameName().compare(name) == 0) {
frames[i]->setFocus();
page()->setCurrentFrameParent(frames[i]->parentFrame());
2011-04-20 23:02:20 +00:00
success();
return;
}
}
2011-04-20 23:02:20 +00:00
frameNotFound();
}
2011-04-20 23:02:20 +00:00
void FrameFocus::focusParent() {
// if (page()->currentFrame()->parentFrame() == 0) {
if (page()->currentFrameParent() == 0) {
2013-02-11 23:31:41 +00:00
finish(false, new ErrorMessage("Already at parent frame."));
2011-04-20 23:02:20 +00:00
} else {
// page()->currentFrame()->parentFrame()->setFocus();
page()->currentFrameParent()->setFocus();
page()->setCurrentFrameParent(page()->currentFrameParent()->parentFrame());
2011-04-20 23:02:20 +00:00
success();
}
}
void FrameFocus::frameNotFound() {
2013-02-11 23:31:41 +00:00
finish(false, new ErrorMessage("Unable to locate frame."));
2011-04-20 23:02:20 +00:00
}
void FrameFocus::success() {
2013-02-11 23:31:41 +00:00
finish(true);
2011-04-20 23:02:20 +00:00
}