File: ui.h

package info (click to toggle)
fityk 0.4.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,472 kB
  • ctags: 2,617
  • sloc: cpp: 19,705; sh: 5,965; xml: 2,325; yacc: 356; makefile: 183; lex: 178
file content (113 lines) | stat: -rw-r--r-- 4,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is part of fityk program. Copyright (C) Marcin Wojdyr
// $Id: ui.h,v 1.4 2004/06/30 10:06:11 wojdyr Exp $


#ifndef ui__h__
#define ui__h__
#include "common.h"
#include <vector>
#include "dotset.h"

class wxString;
struct NumberedLine;


// return value: false -> quit
bool parser(std::string cmd); 


/// A Singleton class.
/// Some methods (plot, plotNow, wait, execCommand, showMessage) 
/// are different and defined separatly for GUI and CLI versions.
/// The program is always linked only with one version of each method.
class UserInterface : public DotSet
{
public:
    /// get Singleton class instance
    static UserInterface* getInstance();

    /// Update plot (if a is given, use it as parameters vector for functions)
    /// Updates only if pri<=auto_plot.   If !now, update can be delayed
    /// Different definition for GUI and CLI
    void drawPlot (int pri=0, bool now=false, const std::vector<fp>& a = fp_v0);

    /// sent message - to user input and to log file (if logging is on)
    void outputMessage (int level, const std::string& s);

    /// Wait and disable UI for ... seconds. Different for GUI and CLI.
    void wait(float seconds); 

    void startLog (char mode, const std::string& filename);
    void stopLog();
    std::string getLogInfo() const;
    char getLogMode() const { return log_mode; };
    std::string getLogFilename() const { return log_filename; };

    /// Excute all commands (or these from specified lines) from file. 
    /// In other words, run a script (.fit).
    void execScript (const std::string& filename, 
                     const std::vector<int>& selected_lines=int_v0);

    void execAndLogCmd(const std::string& s) {log_input(s); execCommand(s);}
    int getVerbosity() { return verbosity; }
    //exit UI and program
    void close();

private:
    UserInterface();
    UserInterface (const UserInterface&); //disable
    UserInterface& operator= (const UserInterface&); //disable

    void doDrawPlot (bool now=false, const std::vector<fp>& a = fp_v0);
    /// show message to user; different definition for GUI and CLI
    void showMessage (OutputStyle style, const std::string& s);

    /// Execute command(s) from string; different definition for GUI and CLI.
    /// It can finish the program (eg. if s=="quit").
    void execCommand (const std::string& s);

    void log_output (const std::string& s);
    void log_input (const std::string& s) 
           { if (log_mode=='i' || log_mode=='a')  logfile << " " << s << "\n"; }

    static UserInterface* instance;
    char log_mode; //i, a, o, n  //TODO: change to enum 
    std::string log_filename;
    std::ofstream logfile;
    char verbosity;
    bool exit_on_warning;
    char auto_plot;
    std::map<char, std::string> autoplot_enum;
    std::map<char, std::string> verbosity_enum;
};


    
extern const char* startup_commands_filename;
extern const char* config_dirname;

inline UserInterface* getUI() { return UserInterface::getInstance(); }

/// execute command(s) from string
inline void exec_command (const std::string& s) { getUI()->execAndLogCmd(s); }


// small utilities for outputing messages to active UI.
/// the smaller level - more important message 
inline void gmessage (int level, const std::string& str) 
                                    { getUI()->outputMessage(level, str); }
//
/// Send warning to UI. Message priority: 1 (lower - more important)
inline void warn(const std::string& s) { gmessage (1, s); }
/// Send message to UI. Message priority: 2 (lower - more important)
inline void mesg(const std::string& s) { gmessage(2, s); }
/// Send information to UI. Message priority: 3 (lower - more important)
inline void info(const std::string& s) { gmessage (3, s); }
/// Send verbose message to UI. Message priority: 4 (lower - more important)
inline void verbose(const std::string& s) { gmessage (4, s); }
/// Send very verbose message to UI. Message priority: 5 (the least important)
inline void very_verbose(const std::string& s) { gmessage (5, s); }
/// The same as verbose(), but string is evaluated only when needed.
#define verbose_lazy(x)   if(getUI()->getVerbosity() >= 4)  verbose((x));  

#endif //ui__h__