xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 46d4471af7fad4e52cfbd09174598cf5318aceed)
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 <LayoutBuilder.h>
18 #include <MenuField.h>
19 #include <MenuItem.h>
20 #include <Message.h>
21 #include <PopUpMenu.h>
22 #include <Slider.h>
23 #include <StringView.h>
24 #include <TextView.h>
25 
26 #include "webp/encode.h"
27 
28 #include "TranslatorSettings.h"
29 #include "WebPTranslator.h"
30 
31 
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "ConfigView"
34 
35 
36 static const uint32 kMsgQuality	= 'qlty';
37 static const uint32 kMsgPreset	= 'prst';
38 static const uint32 kMsgMethod	= 'metd';
39 static const uint32 kMsgPreprocessing = 'pprc';
40 
41 static const struct preset_name {
42 	const char*	name;
43 	WebPPreset	id;
44 } kPresetNames[] = {
45 	{ B_TRANSLATE("Default"), 	WEBP_PRESET_DEFAULT },
46 	{ B_TRANSLATE("Picture"), 	WEBP_PRESET_PICTURE },
47 	{ B_TRANSLATE("Photo"), 	WEBP_PRESET_PHOTO },
48 	{ B_TRANSLATE("Drawing"), 	WEBP_PRESET_DRAWING },
49 	{ B_TRANSLATE("Icon"), 		WEBP_PRESET_ICON },
50 	{ B_TRANSLATE("Text"), 		WEBP_PRESET_TEXT },
51 	{ NULL },
52 };
53 
54 
55 ConfigView::ConfigView(TranslatorSettings* settings)
56 	: BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL),
57 	fSettings(settings)
58 {
59 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
60 
61 	BStringView* title = new BStringView("title",
62 		B_TRANSLATE("WebP image translator"));
63 	title->SetFont(be_bold_font);
64 
65 	char versionString[256];
66 	sprintf(versionString, "v%d.%d.%d",
67 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
68 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
69 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
70 			WEBP_TRANSLATOR_VERSION)));
71 
72 	BStringView* version = new BStringView("version", versionString);
73 
74 	BString copyrightsText;
75 	copyrightsText << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc.")
76 		<< "\n" << B_TRANSLATE("Based on libwebp v0.1,"	)
77 		<< "\n" << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc.");
78 
79 	BTextView* copyrights = new BTextView("copyrights");
80 	copyrights->SetText(copyrightsText);
81 	copyrights->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
82 	copyrights->MakeEditable(false);
83 
84 	// output parameters
85 
86 	fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
87 	const struct preset_name* preset = kPresetNames;
88 	while (preset->name != NULL) {
89 		BMessage* msg = new BMessage(kMsgPreset);
90 		msg->AddInt32("value", preset->id);
91 
92 		BMenuItem* item = new BMenuItem(preset->name, msg);
93 		if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
94 			item->SetMarked(true);
95 		fPresetsMenu->AddItem(item);
96 
97 		preset++;
98 	}
99 	BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
100 		fPresetsMenu);
101 
102 	fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
103 		new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
104 	fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
105 	fQualitySlider->SetHashMarkCount(10);
106 	fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
107 	fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
108 
109 	fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
110 		new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
111 	fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
112 	fMethodSlider->SetHashMarkCount(7);
113 	fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
114 		B_TRANSLATE("Slower but better"));
115 	fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
116 
117 	fPreprocessingCheckBox = new BCheckBox("preprocessing",
118 		B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
119 	if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
120 		fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
121 
122 	// Build the layout
123 	BLayoutBuilder::Group<> builder(GroupLayout());
124 	builder
125 		.SetInsets(5)
126 		.AddGroup(B_HORIZONTAL)
127 			.Add(title)
128 			.Add(version)
129 			.AddGlue()
130 		.End()
131 		.Add(copyrights)
132 		.AddGlue()
133 
134 		.Add(presetsField)
135 		.Add(fQualitySlider)
136 		.Add(fMethodSlider)
137 		.Add(fPreprocessingCheckBox);
138 
139 }
140 
141 
142 ConfigView::~ConfigView()
143 {
144 	fSettings->Release();
145 }
146 
147 
148 void
149 ConfigView::AttachedToWindow()
150 {
151 	BGroupView::AttachedToWindow();
152 
153 	fPresetsMenu->SetTargetForItems(this);
154 
155 	fQualitySlider->SetTarget(this);
156 	fMethodSlider->SetTarget(this);
157 	fPreprocessingCheckBox->SetTarget(this);
158 
159 	if (Parent() == NULL && Window()->GetLayout() == NULL) {
160 		Window()->SetLayout(new BGroupLayout(B_VERTICAL));
161 		Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
162 	}
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 		BGroupView::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