From 3615624ec44b458a6c5771adf0ca5c61c77a57b0 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Wed, 20 Apr 2011 19:02:20 -0400 Subject: [PATCH] Refactored FrameFocus --- src/FrameFocus.cpp | 96 ++++++++++++++++++++++++++++------------------ src/FrameFocus.h | 16 ++++++++ 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/FrameFocus.cpp b/src/FrameFocus.cpp index 5720bbc..af8bf30 100644 --- a/src/FrameFocus.cpp +++ b/src/FrameFocus.cpp @@ -6,44 +6,64 @@ FrameFocus::FrameFocus(WebPage *page, QObject *parent) : Command(page, parent) { } void FrameFocus::start(QStringList &arguments) { - int index; - bool ok; - bool found = false; - QString response; - QList child_frames = page()->currentFrame()->childFrames(); - if (arguments.length() > 0) { - if (arguments.length() > 1) { - // Find frame by index. - index = arguments[1].toInt(&ok); - if (ok && 0 <= index && index < child_frames.length()) { - child_frames[index]->setFocus(); - found = true; - } - } else { - // Find frame by id. - for (int i = 0; i < child_frames.length(); i++) { - if (child_frames[i]->frameName().compare(arguments[0]) == 0) { - child_frames[i]->setFocus(); - found = true; - break; - } - } - } - if (found) { - emit finished(true, response); - } else { - response = "Unable to locate frame. "; - emit finished(false, response); - } - } else { - // Set focus on parent. - if (page()->currentFrame()->parentFrame() == 0) { - response = "Already at parent frame."; - emit finished(false, response); - } else { - page()->currentFrame()->parentFrame()->setFocus(); - emit finished(true, response); - } + findFrames(); + switch(arguments.length()) { + case 1: + focusId(arguments[0]); + break; + case 2: + focusIndex(arguments[1].toInt()); + break; + default: + focusParent(); } } +void FrameFocus::findFrames() { + frames = page()->currentFrame()->childFrames(); +} + +void FrameFocus::focusIndex(int index) { + if (isFrameAtIndex(index)) { + frames[index]->setFocus(); + success(); + } else { + 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; + } + } + + frameNotFound(); +} + +void FrameFocus::focusParent() { + if (page()->currentFrame()->parentFrame() == 0) { + QString response = "Already at parent frame."; + emit finished(false, response); + } else { + page()->currentFrame()->parentFrame()->setFocus(); + success(); + } +} + +void FrameFocus::frameNotFound() { + QString response = "Unable to locate frame. "; + emit finished(false, response); +} + +void FrameFocus::success() { + QString response; + emit finished(true, response); +} diff --git a/src/FrameFocus.h b/src/FrameFocus.h index a54f429..96a57d2 100644 --- a/src/FrameFocus.h +++ b/src/FrameFocus.h @@ -1,6 +1,7 @@ #include "Command.h" class WebPage; +class QWebFrame; class FrameFocus : public Command { Q_OBJECT @@ -8,5 +9,20 @@ class FrameFocus : public Command { public: FrameFocus(WebPage *page, QObject *parent = 0); virtual void start(QStringList &arguments); + + private: + void findFrames(); + + void focusParent(); + + void focusIndex(int index); + bool isFrameAtIndex(int index); + + void focusId(QString id); + + void success(); + void frameNotFound(); + + QList frames; };