Polymorphic AppController for cli and gui modes.

This commit is contained in:
tamasmeszaros 2018-09-19 18:02:04 +02:00
parent c9acd1252a
commit 2306c1589a
8 changed files with 338 additions and 183 deletions

View file

@ -20,6 +20,21 @@ class PrintConfig;
class ProgressStatusBar;
class DynamicPrintConfig;
/// A Progress indicator object smart pointer
using ProgresIndicatorPtr = std::shared_ptr<ProgressIndicator>;
using FilePath = std::string;
using FilePathList = std::vector<FilePath>;
/// Common runtime issue types
enum class IssueType {
INFO,
WARN,
WARN_Q, // Warning with a question to continue
ERR,
FATAL
};
/**
* @brief A boilerplate class for creating application logic. It should provide
* features as issue reporting and progress indication, etc...
@ -32,34 +47,12 @@ class DynamicPrintConfig;
* UI toolkit dependencies. We can implement it with any UI framework or make it
* a cli client.
*/
class AppControllerBoilerplate {
class AppControllerBase {
public:
/// A Progress indicator object smart pointer
using ProgresIndicatorPtr = std::shared_ptr<ProgressIndicator>;
using Ptr = std::shared_ptr<AppControllerBase>;
private:
class PriData; // Some structure to store progress indication data
// Pimpl data for thread safe progress indication features
std::unique_ptr<PriData> m_pri_data;
public:
AppControllerBoilerplate();
~AppControllerBoilerplate();
using Path = std::string;
using PathList = std::vector<Path>;
/// Common runtime issue types
enum class IssueType {
INFO,
WARN,
WARN_Q, // Warning with a question to continue
ERR,
FATAL
};
inline virtual ~AppControllerBase() {}
/**
* @brief Query some paths from the user.
@ -69,23 +62,28 @@ public:
* @param extensions Recognized file extensions.
* @return Returns a list of paths chosen by the user.
*/
PathList query_destination_paths(
virtual FilePathList query_destination_paths(
const std::string& title,
const std::string& extensions) const;
const std::string& extensions,
const std::string& functionid = "",
const std::string& hint = "") const = 0;
/**
* @brief Same as query_destination_paths but works for directories only.
*/
PathList query_destination_dirs(
const std::string& title) const;
virtual FilePathList query_destination_dirs(
const std::string& title,
const std::string& functionid = "",
const std::string& hint = "") const = 0;
/**
* @brief Same as query_destination_paths but returns only one path.
*/
Path query_destination_path(
virtual FilePath query_destination_path(
const std::string& title,
const std::string& extensions,
const std::string& hint = "") const;
const std::string& functionid = "",
const std::string& hint = "") const = 0;
/**
* @brief Report an issue to the user be it fatal or recoverable.
@ -97,12 +95,9 @@ public:
* @param brief A very brief description. Can be used for message dialog
* title.
*/
bool report_issue(IssueType issuetype,
const std::string& description,
const std::string& brief);
bool report_issue(IssueType issuetype,
const std::string& description);
virtual bool report_issue(IssueType issuetype,
const std::string& description,
const std::string& brief) = 0;
/**
* @brief Return the global progress indicator for the current controller.
@ -110,9 +105,9 @@ public:
*
* Only one thread should use the global indicator at a time.
*/
ProgresIndicatorPtr global_progress_indicator();
virtual ProgresIndicatorPtr global_progress_indicator() = 0;
void global_progress_indicator(ProgresIndicatorPtr gpri);
virtual void global_progress_indicator(ProgresIndicatorPtr gpri) = 0;
/**
* @brief A predicate telling the caller whether it is the thread that
@ -122,7 +117,7 @@ public:
* @return Return true for the same caller thread that created this
* object and false for every other.
*/
bool is_main_thread() const;
virtual bool is_main_thread() const = 0;
/**
* @brief The frontend supports asynch execution.
@ -138,11 +133,9 @@ public:
* @return true if a job or method can be executed asynchronously, false
* otherwise.
*/
bool supports_asynch() const;
virtual bool supports_asynch() const = 0;
void process_events();
protected:
virtual void process_events() = 0;
/**
* @brief Create a new progress indicator and return a smart pointer to it.
@ -151,14 +144,138 @@ protected:
* @param firstmsg The message for the first subtask to be displayed.
* @return Smart pointer to the created object.
*/
ProgresIndicatorPtr create_progress_indicator(
virtual ProgresIndicatorPtr create_progress_indicator(
unsigned statenum,
const std::string& title,
const std::string& firstmsg) const;
const std::string& firstmsg = "") const = 0;
};
ProgresIndicatorPtr create_progress_indicator(
/**
* @brief Implementation of AppControllerBase for the GUI app
*/
class AppControllerGui: public AppControllerBase {
private:
class PriData; // Some structure to store progress indication data
// Pimpl data for thread safe progress indication features
std::unique_ptr<PriData> m_pri_data;
public:
AppControllerGui();
virtual ~AppControllerGui();
virtual FilePathList query_destination_paths(
const std::string& title,
const std::string& extensions,
const std::string& functionid,
const std::string& hint) const override;
virtual FilePathList query_destination_dirs(
const std::string& /*title*/,
const std::string& /*functionid*/,
const std::string& /*hint*/) const override { return {}; }
virtual FilePath query_destination_path(
const std::string& title,
const std::string& extensions,
const std::string& functionid,
const std::string& hint) const override;
virtual bool report_issue(IssueType issuetype,
const std::string& description,
const std::string& brief = std::string()) override;
virtual ProgresIndicatorPtr global_progress_indicator() override;
virtual void global_progress_indicator(ProgresIndicatorPtr gpri) override;
virtual bool is_main_thread() const override;
virtual bool supports_asynch() const override;
virtual void process_events() override;
virtual ProgresIndicatorPtr create_progress_indicator(
unsigned statenum,
const std::string& title) const;
const std::string& title,
const std::string& firstmsg) const override;
protected:
// This is a global progress indicator placeholder. In the Slic3r UI it can
// contain the progress indicator on the statusbar.
ProgresIndicatorPtr m_global_progressind;
};
class AppControllerCli: public AppControllerBase {
class CliProgress : public ProgressIndicator {
std::string m_msg, m_title;
public:
virtual void message(const std::string& msg) override {
m_msg = msg;
}
virtual void title(const std::string& title) override {
m_title = title;
}
};
public:
AppControllerCli() {
std::cout << "Cli AppController ready..." << std::endl;
m_global_progressind = std::make_shared<CliProgress>();
}
virtual ~AppControllerCli() {}
virtual FilePathList query_destination_paths(
const std::string& /*title*/,
const std::string& /*extensions*/,
const std::string& /*functionid*/,
const std::string& /*hint*/) const override { return {}; }
virtual FilePathList query_destination_dirs(
const std::string& /*title*/,
const std::string& /*functionid*/,
const std::string& /*hint*/) const override { return {}; }
virtual FilePath query_destination_path(
const std::string& /*title*/,
const std::string& /*extensions*/,
const std::string& /*functionid*/,
const std::string& /*hint*/) const override { return "out.zip"; }
virtual bool report_issue(IssueType /*issuetype*/,
const std::string& description,
const std::string& brief) override {
std::cerr << brief << ": " << description << std::endl;
return true;
}
virtual ProgresIndicatorPtr global_progress_indicator() override {
return m_global_progressind;
}
virtual void global_progress_indicator(ProgresIndicatorPtr) override {}
virtual bool is_main_thread() const override { return true; }
virtual bool supports_asynch() const override { return false; }
virtual void process_events() override {}
virtual ProgresIndicatorPtr create_progress_indicator(
unsigned /*statenum*/,
const std::string& /*title*/,
const std::string& /*firstmsg*/) const override {
return std::make_shared<CliProgress>();
}
protected:
// This is a global progress indicator placeholder. In the Slic3r UI it can
// contain the progress indicator on the statusbar.
@ -185,7 +302,7 @@ public:
/**
* @brief Implementation of the printing logic.
*/
class PrintController: public AppControllerBoilerplate {
class PrintController {
Print *m_print = nullptr;
std::function<void()> m_rempools;
protected:
@ -241,7 +358,7 @@ public:
/**
* @brief Top level controller.
*/
class AppController: public AppControllerBoilerplate {
class AppController {
Model *m_model = nullptr;
PrintController::Ptr printctl;
std::atomic<bool> m_arranging;