xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
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, uint32 flags)
56 	: BView(B_TRANSLATE("WebPTranslator Settings"), flags),
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<>(this, B_VERTICAL)
124 		.SetInsets(5)
125 		.AddGroup(B_HORIZONTAL)
126 			.Add(title)
127 			.Add(version)
128 			.AddGlue()
129 		.End()
130 		.Add(copyrights)
131 		.AddGlue()
132 
133 		.AddGrid()
134 			.Add(presetsField->CreateLabelLayoutItem(), 0, 0)
135 			.Add(presetsField->CreateMenuBarLayoutItem(), 1, 0)
136 		.End()
137 		.Add(fQualitySlider)
138 		.Add(fMethodSlider)
139 		.Add(fPreprocessingCheckBox);
140 }
141 
142 
143 ConfigView::~ConfigView()
144 {
145 	fSettings->Release();
146 }
147 
148 
149 void
150 ConfigView::AttachedToWindow()
151 {
152 	BView::AttachedToWindow();
153 
154 	fPresetsMenu->SetTargetForItems(this);
155 
156 	fQualitySlider->SetTarget(this);
157 	fMethodSlider->SetTarget(this);
158 	fPreprocessingCheckBox->SetTarget(this);
159 }
160 
161 
162 void
163 ConfigView::MessageReceived(BMessage* message)
164 {
165 	struct {
166 		const char*		name;
167 		uint32			what;
168 		TranSettingType	type;
169 	} maps[] = {
170 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
171 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
172 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
173 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
174 		{ NULL }
175 	};
176 
177 	int i;
178 	for (i = 0; maps[i].name != NULL; i++) {
179 		if (maps[i].what == message->what)
180 			break;
181 	}
182 
183 	if (maps[i].name == NULL) {
184 		BView::MessageReceived(message);
185 		return;
186 	}
187 
188 	int32 value;
189 	if (message->FindInt32("value", &value) == B_OK
190 		|| message->FindInt32("be:value", &value) == B_OK) {
191 		switch(maps[i].type) {
192 			case TRAN_SETTING_BOOL:
193 			{
194 				bool boolValue = value;
195 				fSettings->SetGetBool(maps[i].name, &boolValue);
196 				break;
197 			}
198 			case TRAN_SETTING_INT32:
199 				fSettings->SetGetInt32(maps[i].name, &value);
200 				break;
201 		}
202 		fSettings->SaveSettings();
203 	}
204 }
205