capybara-webkit/src/FrameFocus.cpp

68 lines
1.4 KiB
C++
Raw Normal View History

#include "FrameFocus.h"
#include "Command.h"
#include "WebPage.h"
#include "WebPageManager.h"
FrameFocus::FrameFocus(WebPageManager *manager, QStringList &arguments, QObject *parent) : Command(manager, arguments, parent) {
}
void FrameFocus::start() {
2011-04-20 23:02:20 +00:00
findFrames();
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) {
if (isFrameAtIndex(index)) {
frames[index]->setFocus();
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) {
for (int i = 0; i < frames.length(); i++) {
if (frames[i]->frameName().compare(name) == 0) {
frames[i]->setFocus();
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) {
emit finished(new Response(false, QString("Already at parent frame.")));
2011-04-20 23:02:20 +00:00
} else {
page()->currentFrame()->parentFrame()->setFocus();
success();
}
}
void FrameFocus::frameNotFound() {
emit finished(new Response(false, QString("Unable to locate frame. ")));
2011-04-20 23:02:20 +00:00
}
void FrameFocus::success() {
emit finished(new Response(true));
2011-04-20 23:02:20 +00:00
}