xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 323b65468e5836bb27a5e373b14027d902349437)
1 /*
2  * Copyright 2010-2011, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Philippe Houdoin
7  */
8 
9 
10 #include "ConfigView.h"
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <Catalog.h>
16 #include <CheckBox.h>
17 #include <GridLayoutBuilder.h>
18 #include <GroupLayout.h>
19 #include <GroupLayoutBuilder.h>
20 #include <MenuField.h>
21 #include <MenuItem.h>
22 #include <Message.h>
23 #include <PopUpMenu.h>
24 #include <Slider.h>
25 #include <StringView.h>
26 #include <TextView.h>
27 
28 #include "webp/encode.h"
29 
30 #include "TranslatorSettings.h"
31 #include "WebPTranslator.h"
32 
33 
34 #undef B_TRANSLATE_CONTEXT
35 #define B_TRANSLATE_CONTEXT "ConfigView"
36 
37 
38 static const uint32 kMsgQuality	= 'qlty';
39 static const uint32 kMsgPreset	= 'prst';
40 static const uint32 kMsgMethod	= 'metd';
41 static const uint32 kMsgPreprocessing = 'pprc';
42 
43 static const struct preset_name {
44 	const char*	name;
45 	WebPPreset	id;
46 } kPresetNames[] = {
47 	{ B_TRANSLATE("Default"), 	WEBP_PRESET_DEFAULT },
48 	{ B_TRANSLATE("Picture"), 	WEBP_PRESET_PICTURE },
49 	{ B_TRANSLATE("Photo"), 	WEBP_PRESET_PHOTO },
50 	{ B_TRANSLATE("Drawing"), 	WEBP_PRESET_DRAWING },
51 	{ B_TRANSLATE("Icon"), 		WEBP_PRESET_ICON },
52 	{ B_TRANSLATE("Text"), 		WEBP_PRESET_TEXT },
53 	{ NULL },
54 };
55 
56 
57 ConfigView::ConfigView(TranslatorSettings* settings, uint32 flags)
58 	: BView(B_TRANSLATE("WebPTranslator Settings"), flags),
59 	fSettings(settings)
60 {
61 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
62 
63 	BStringView* title = new BStringView("title", B_TRANSLATE("WebP Images"));
64 	title->SetFont(be_bold_font);
65 
66 	char versionString[256];
67 	sprintf(versionString, "v%d.%d.%d",
68 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
69 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
70 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
71 			WEBP_TRANSLATOR_VERSION)));
72 
73 	BStringView* version = new BStringView("version", versionString);
74 
75 	BString copyrightsText;
76 	copyrightsText << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc.")
77 		<< "\n" << B_TRANSLATE("Based on libwebp v0.1,"	)
78 		<< "\n" << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc.");
79 
80 	BTextView* copyrights = new BTextView("copyrights");
81 	copyrights->SetText(copyrightsText);
82 	copyrights->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
83 	copyrights->MakeEditable(false);
84 
85 	// output parameters
86 
87 	fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
88 	const struct preset_name* preset = kPresetNames;
89 	while (preset->name != NULL) {
90 		BMessage* msg = new BMessage(kMsgPreset);
91 		msg->AddInt32("value", preset->id);
92 
93 		BMenuItem* item = new BMenuItem(preset->name, msg);
94 		if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
95 			item->SetMarked(true);
96 		fPresetsMenu->AddItem(item);
97 
98 		preset++;
99 	}
100 	BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
101 		fPresetsMenu);
102 
103 	fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
104 		new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
105 	fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
106 	fQualitySlider->SetHashMarkCount(10);
107 	fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
108 	fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
109 
110 	fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
111 		new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
112 	fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
113 	fMethodSlider->SetHashMarkCount(7);
114 	fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
115 		B_TRANSLATE("Slower but better"));
116 	fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
117 
118 	fPreprocessingCheckBox = new BCheckBox("preprocessing",
119 		B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
120 	if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
121 		fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
122 
123 	// Build the layout
124 	SetLayout(new BGroupLayout(B_VERTICAL));
125 
126 	AddChild(BGroupLayoutBuilder(B_VERTICAL)
127 		.Add(BGroupLayoutBuilder(B_HORIZONTAL)
128 			.Add(title)
129 			.Add(version)
130 			.AddGlue()
131 		)
132 		.Add(copyrights)
133 		.AddGlue()
134 
135 		.Add(BGridLayoutBuilder()
136 			.Add(presetsField->CreateLabelLayoutItem(), 0, 0)
137 			.Add(presetsField->CreateMenuBarLayoutItem(), 1, 0)
138 		)
139 		.Add(fQualitySlider)
140 		.Add(fMethodSlider)
141 		.Add(fPreprocessingCheckBox)
142 		.SetInsets(5, 5, 5, 5)
143 	);
144 }
145 
146 
147 ConfigView::~ConfigView()
148 {
149 	fSettings->Release();
150 }
151 
152 
153 void
154 ConfigView::AttachedToWindow()
155 {
156 	BView::AttachedToWindow();
157 
158 	fPresetsMenu->SetTargetForItems(this);
159 
160 	fQualitySlider->SetTarget(this);
161 	fMethodSlider->SetTarget(this);
162 	fPreprocessingCheckBox->SetTarget(this);
163 }
164 
165 
166 void
167 ConfigView::MessageReceived(BMessage* message)
168 {
169 	struct {
170 		const char*		name;
171 		uint32			what;
172 		TranSettingType	type;
173 	} maps[] = {
174 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
175 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
176 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
177 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
178 		{ NULL }
179 	};
180 
181 	int i;
182 	for (i = 0; maps[i].name != NULL; i++) {
183 		if (maps[i].what == message->what)
184 			break;
185 	}
186 
187 	if (maps[i].name == NULL) {
188 		BView::MessageReceived(message);
189 		return;
190 	}
191 
192 	int32 value;
193 	if (message->FindInt32("value", &value) == B_OK
194 		|| message->FindInt32("be:value", &value) == B_OK) {
195 		switch(maps[i].type) {
196 			case TRAN_SETTING_BOOL:
197 			{
198 				bool boolValue = value;
199 				fSettings->SetGetBool(maps[i].name, &boolValue);
200 				break;
201 			}
202 			case TRAN_SETTING_INT32:
203 				fSettings->SetGetInt32(maps[i].name, &value);
204 				break;
205 		}
206 		fSettings->SaveSettings();
207 	}
208 }
209