xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
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", B_TRANSLATE("WebP Images"));
62 	title->SetFont(be_bold_font);
63 
64 	char versionString[256];
65 	sprintf(versionString, "v%d.%d.%d",
66 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WEBP_TRANSLATOR_VERSION)),
67 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WEBP_TRANSLATOR_VERSION)),
68 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
69 			WEBP_TRANSLATOR_VERSION)));
70 
71 	BStringView* version = new BStringView("version", versionString);
72 
73 	BString copyrightsText;
74 	copyrightsText << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc.")
75 		<< "\n" << B_TRANSLATE("Based on libwebp v0.1,"	)
76 		<< "\n" << B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc.");
77 
78 	BTextView* copyrights = new BTextView("copyrights");
79 	copyrights->SetText(copyrightsText);
80 	copyrights->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
81 	copyrights->MakeEditable(false);
82 
83 	// output parameters
84 
85 	fPresetsMenu = new BPopUpMenu(B_TRANSLATE("Preset"));
86 	const struct preset_name* preset = kPresetNames;
87 	while (preset->name != NULL) {
88 		BMessage* msg = new BMessage(kMsgPreset);
89 		msg->AddInt32("value", preset->id);
90 
91 		BMenuItem* item = new BMenuItem(preset->name, msg);
92 		if (fSettings->SetGetInt32(WEBP_SETTING_PRESET) == preset->id)
93 			item->SetMarked(true);
94 		fPresetsMenu->AddItem(item);
95 
96 		preset++;
97 	}
98 	BMenuField* presetsField = new BMenuField(B_TRANSLATE("Output preset:"),
99 		fPresetsMenu);
100 
101 	fQualitySlider = new BSlider("quality", B_TRANSLATE("Output quality:"),
102 		new BMessage(kMsgQuality), 0, 100, B_HORIZONTAL, B_BLOCK_THUMB);
103 	fQualitySlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
104 	fQualitySlider->SetHashMarkCount(10);
105 	fQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
106 	fQualitySlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_QUALITY));
107 
108 	fMethodSlider = new BSlider("method", B_TRANSLATE("Compression method:"),
109 		new BMessage(kMsgMethod), 0, 6, B_HORIZONTAL, B_BLOCK_THUMB);
110 	fMethodSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
111 	fMethodSlider->SetHashMarkCount(7);
112 	fMethodSlider->SetLimitLabels(B_TRANSLATE("Fast"),
113 		B_TRANSLATE("Slower but better"));
114 	fMethodSlider->SetValue(fSettings->SetGetInt32(WEBP_SETTING_METHOD));
115 
116 	fPreprocessingCheckBox = new BCheckBox("preprocessing",
117 		B_TRANSLATE("Preprocessing filter"), new BMessage(kMsgPreprocessing));
118 	if (fSettings->SetGetBool(WEBP_SETTING_PREPROCESSING))
119 		fPreprocessingCheckBox->SetValue(B_CONTROL_ON);
120 
121 	// Build the layout
122 	BLayoutBuilder::Group<>(this, B_VERTICAL)
123 		.SetInsets(5)
124 		.AddGroup(B_HORIZONTAL)
125 			.Add(title)
126 			.Add(version)
127 			.AddGlue()
128 		.End()
129 		.Add(copyrights)
130 		.AddGlue()
131 
132 		.AddGrid()
133 			.Add(presetsField->CreateLabelLayoutItem(), 0, 0)
134 			.Add(presetsField->CreateMenuBarLayoutItem(), 1, 0)
135 		.End()
136 		.Add(fQualitySlider)
137 		.Add(fMethodSlider)
138 		.Add(fPreprocessingCheckBox);
139 }
140 
141 
142 ConfigView::~ConfigView()
143 {
144 	fSettings->Release();
145 }
146 
147 
148 void
149 ConfigView::AttachedToWindow()
150 {
151 	BView::AttachedToWindow();
152 
153 	fPresetsMenu->SetTargetForItems(this);
154 
155 	fQualitySlider->SetTarget(this);
156 	fMethodSlider->SetTarget(this);
157 	fPreprocessingCheckBox->SetTarget(this);
158 }
159 
160 
161 void
162 ConfigView::MessageReceived(BMessage* message)
163 {
164 	struct {
165 		const char*		name;
166 		uint32			what;
167 		TranSettingType	type;
168 	} maps[] = {
169 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
170 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
171 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
172 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
173 		{ NULL }
174 	};
175 
176 	int i;
177 	for (i = 0; maps[i].name != NULL; i++) {
178 		if (maps[i].what == message->what)
179 			break;
180 	}
181 
182 	if (maps[i].name == NULL) {
183 		BView::MessageReceived(message);
184 		return;
185 	}
186 
187 	int32 value;
188 	if (message->FindInt32("value", &value) == B_OK
189 		|| message->FindInt32("be:value", &value) == B_OK) {
190 		switch(maps[i].type) {
191 			case TRAN_SETTING_BOOL:
192 			{
193 				bool boolValue = value;
194 				fSettings->SetGetBool(maps[i].name, &boolValue);
195 				break;
196 			}
197 			case TRAN_SETTING_INT32:
198 				fSettings->SetGetInt32(maps[i].name, &value);
199 				break;
200 		}
201 		fSettings->SaveSettings();
202 	}
203 }
204