summaryrefslogtreecommitdiff
path: root/plugingui/abouttab.cc
blob: a04fac2d5db18145eceb99dd2e83edf97572f95f (plain)
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
/* -*- Mode: c++ -*- */
/***************************************************************************
 *            abouttab.cc
 *
 *  Fri Apr 21 18:51:13 CEST 2017
 *  Copyright 2017 André Nusser
 *  andre.nusser@googlemail.com
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "abouttab.h"

#include <version.h>

#include "utf8.h"

namespace GUI
{

//!Simple helper class to avoid direct repeats of text manipulation.
class AboutTextBuilder {
public:
	//!Class constructor. Sets the separator character.
	AboutTextBuilder(char _separator='=')
    :separator_character(_separator)
	{
	}

	//!Adds a new section to the "about" text with the given title and contents.
	//!Returns a reference to the builder so more calls can be chained.
	AboutTextBuilder& add(const std::string& _title, const std::string& _contents) 
	{
		auto repeat=[](std::string& _str, char _fill, size_t _count) 
		{
			_str.insert(_str.size(), _count, _fill);
		};

		repeat(about_text, separator_character, separator_count);
		about_text+="\n";
		repeat(about_text, ' ', space_count);
		about_text+=_title+"\n";
		repeat(about_text, separator_character, separator_count);
		repeat(about_text, '\n', 3);
		about_text+=_contents+"\n";

		return *this;
	}

	//!Returns the built string.
	const std::string& get() const 
	{
		return about_text;
	}

private:
	const size_t separator_count=13; //!< Number of times that the separator character will be added. 
	const size_t space_count=13; //!< Number of times that the space character will be added before the title.
	char separator_character; //!< Separator character. Defaults to '='.
	std::string about_text; //!< Text that will be filled up with calls to "add".
};

AboutTab::AboutTab(Widget* parent)
	: Widget(parent)
{
	text_edit.setText(getAboutText());
	text_edit.setReadOnly(true);
	text_edit.resize(std::max((int)width() - 2*margin,0),
	                 std::max((int)height() - 2*margin, 0));
	text_edit.move(margin, margin);
}

void AboutTab::resize(std::size_t width, std::size_t height)
{
	Widget::resize(width, height);
	text_edit.resize(std::max((int)width - 2*margin, 0),
	                 std::max((int)height - 2*margin, 0));
}

std::string AboutTab::getAboutText()
{
	AboutTextBuilder builder('*');

	//This will casually add an extra newline at the end of License.
	return builder.add("About", about.data())
	    .add("Version", std::string(VERSION)+"\n")
	    .add("Bugs", bugs.data())
	    .add("Authors", UTF8().toLatin1(authors.data()))
	    .add("License", gpl.data())
	    .get();
}

} // GUI::