b15f/control/src/ui/view.h

91 lines
2.5 KiB
C
Raw Normal View History

2019-04-04 11:19:11 +00:00
#ifndef VIEW_H
#define VIEW_H
#include <iostream>
#include <cmath>
#include <vector>
#include <functional>
#include <ncurses.h> // sudo apt-get install libncurses5-dev
#include <sys/ioctl.h>
#include <unistd.h>
2019-04-04 14:16:28 +00:00
#include <signal.h>
2019-05-09 12:47:47 +00:00
#include "../drv/b15f.h"
2019-04-04 14:16:28 +00:00
extern std::string ERR_MSG;
2019-04-05 12:33:15 +00:00
typedef std::function<void(int)> call_t;
2019-04-04 11:19:11 +00:00
2019-05-23 13:08:54 +00:00
/*! Base class for multiple views with the ncurses user interface. */
2019-04-04 11:19:11 +00:00
class View
{
public:
2019-07-31 09:59:24 +00:00
/**
* standard constructor, takes no args
*/
2019-05-23 13:08:54 +00:00
View(void);
2019-07-31 09:59:24 +00:00
/**
* standard destructor
*/
2019-05-23 13:08:54 +00:00
virtual ~View(void);
2019-07-31 09:59:24 +00:00
/**
* Sets static view context, used for every view
* Note: this UI systems supports only one window
* \param win window context
*/
2019-05-23 13:08:54 +00:00
static void setWinContext(WINDOW* win);
2019-07-31 09:59:24 +00:00
/**
* \return static window context for all views
*/
2019-05-23 13:08:54 +00:00
static WINDOW* getWinContext(void);
2019-07-31 09:59:24 +00:00
/**
* Splits a string by a delimeter and returns the parts in a vector
* \param str input string
* \param delim the delimeter for splitting
* \return splitted parts of str
*/
2019-05-23 13:08:54 +00:00
static std::vector<std::string> str_split(const std::string& str, const std::string delim);
2019-07-31 09:59:24 +00:00
/**
* Sets the title of this view
* \param title string to set title
*/
2019-05-23 13:08:54 +00:00
virtual void setTitle(std::string title);
2019-07-31 09:59:24 +00:00
/**
* Repaints the whole window with the current view.
* The centered position of the window in the terminal gets new calculated.
* The window box gets repainted.
*/
2019-05-23 13:08:54 +00:00
virtual void repaint(void);
2019-07-31 09:59:24 +00:00
/**
* Abstract function for drawing ths particular view.
* Gets overridden in the derived classes to paints for example a promt.
*/
2019-05-23 13:08:54 +00:00
virtual void draw(void) = 0;
2019-07-31 09:59:24 +00:00
/**
* Abstract function for the view to reacted on a keypress.
* Gets overridden in the derived classes to close for example a view on ESC.
*/
2019-05-23 13:08:54 +00:00
virtual call_t keypress(int& key) = 0;
2019-04-04 11:19:11 +00:00
protected:
2019-07-31 09:59:24 +00:00
int width; //!< width of view in terminal characters
int height; //!< height of view in terminal characters
int start_x = 0; //!< x offset (characters) in the terminal, used to center the window on repaint()
int start_y = 0; //!< y offset (characters) in the terminal, used to center the window on repaint()
std::string title; //!< title of the view
std::vector<call_t> calls; //!< calls (function pointers) for different button actions in the view (if any)
2019-05-23 13:08:54 +00:00
2019-07-31 09:59:24 +00:00
static WINDOW* win; //!< static window contexts for all views
constexpr static int KEY_ENT = 10; //!< Key value for the Enter key
2019-04-04 11:19:11 +00:00
};
#endif // VIEW_H