xref: /haiku/src/add-ons/translators/webp/ConfigView.cpp (revision 5e7964b0a929555415798dea3373db9ac4611caa)
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 
25 #include "webp/encode.h"
26 
27 #include "TranslatorSettings.h"
28 #include "WebPTranslator.h"
29 
30 
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "ConfigView"
33 
34 
35 static const uint32 kMsgQuality	= 'qlty';
36 static const uint32 kMsgPreset	= 'prst';
37 static const uint32 kMsgMethod	= 'metd';
38 static const uint32 kMsgPreprocessing = 'pprc';
39 
40 static const struct preset_name {
41 	const char*	name;
42 	WebPPreset	id;
43 } kPresetNames[] = {
44 	{ B_TRANSLATE("Default"), 	WEBP_PRESET_DEFAULT },
45 	{ B_TRANSLATE("Picture"), 	WEBP_PRESET_PICTURE },
46 	{ B_TRANSLATE("Photo"), 	WEBP_PRESET_PHOTO },
47 	{ B_TRANSLATE("Drawing"), 	WEBP_PRESET_DRAWING },
48 	{ B_TRANSLATE("Icon"), 		WEBP_PRESET_ICON },
49 	{ B_TRANSLATE("Text"), 		WEBP_PRESET_TEXT },
50 	{ NULL },
51 };
52 
53 
54 ConfigView::ConfigView(TranslatorSettings* settings)
55 	:
56 	BGroupView(B_TRANSLATE("WebPTranslator Settings"), B_VERTICAL),
57 	fSettings(settings)
58 {
59 	SetViewUIColor(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, %s",
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 		__DATE__);
72 
73 	BStringView* version = new BStringView("version", versionString);
74 
75 	BString copyrightsText;
76 	BStringView *copyrightView = new BStringView("Copyright",
77 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Haiku Inc."));
78 	BStringView *copyright2View = new BStringView("Copyright2",
79 		B_TRANSLATE("Based on libwebp v0.1,"));
80 	BStringView *copyright3View = new BStringView("Copyright3",
81 		B_TRANSLATE(B_UTF8_COPYRIGHT "2010-2011 Google Inc."));
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, 0)
123 		.SetInsets(B_USE_DEFAULT_SPACING)
124 		.Add(title)
125 		.Add(version)
126 		.Add(copyrightView)
127 		.AddGlue()
128 		.AddGrid(B_USE_DEFAULT_SPACING, B_USE_SMALL_SPACING)
129 			.Add(presetsField->CreateLabelLayoutItem(), 0, 0)
130 			.AddGroup(B_HORIZONTAL, 0.0f, 1, 0)
131 				.Add(presetsField->CreateMenuBarLayoutItem(), 0.0f)
132 				.AddGlue()
133 				.End()
134 			.End()
135 		.Add(fQualitySlider)
136 		.Add(fMethodSlider)
137 		.Add(fPreprocessingCheckBox)
138 		.AddGlue()
139 		.Add(copyright2View)
140 		.Add(copyright3View);
141 
142 	BFont font;
143 	GetFont(&font);
144 	SetExplicitPreferredSize(BSize((font.Size() * 250) / 12,
145 		(font.Size() * 350) / 12));
146 }
147 
148 
149 ConfigView::~ConfigView()
150 {
151 	fSettings->Release();
152 }
153 
154 
155 void
156 ConfigView::AttachedToWindow()
157 {
158 	BGroupView::AttachedToWindow();
159 
160 	fPresetsMenu->SetTargetForItems(this);
161 
162 	fQualitySlider->SetTarget(this);
163 	fMethodSlider->SetTarget(this);
164 	fPreprocessingCheckBox->SetTarget(this);
165 
166 	if (Parent() == NULL && Window()->GetLayout() == NULL) {
167 		Window()->SetLayout(new BGroupLayout(B_VERTICAL));
168 		Window()->ResizeTo(PreferredSize().Width(), PreferredSize().Height());
169 	}
170 }
171 
172 
173 void
174 ConfigView::MessageReceived(BMessage* message)
175 {
176 	struct {
177 		const char*		name;
178 		uint32			what;
179 		TranSettingType	type;
180 	} maps[] = {
181 		{ WEBP_SETTING_PRESET, kMsgPreset, TRAN_SETTING_INT32 },
182 		{ WEBP_SETTING_QUALITY, kMsgQuality, TRAN_SETTING_INT32 },
183 		{ WEBP_SETTING_METHOD, kMsgMethod, TRAN_SETTING_INT32 },
184 		{ WEBP_SETTING_PREPROCESSING, kMsgPreprocessing, TRAN_SETTING_BOOL },
185 		{ NULL }
186 	};
187 
188 	int i;
189 	for (i = 0; maps[i].name != NULL; i++) {
190 		if (maps[i].what == message->what)
191 			break;
192 	}
193 
194 	if (maps[i].name == NULL) {
195 		BGroupView::MessageReceived(message);
196 		return;
197 	}
198 
199 	int32 value;
200 	if (message->FindInt32("value", &value) == B_OK
201 		|| message->FindInt32("be:value", &value) == B_OK) {
202 		switch(maps[i].type) {
203 			case TRAN_SETTING_BOOL:
204 			{
205 				bool boolValue = value;
206 				fSettings->SetGetBool(maps[i].name, &boolValue);
207 				break;
208 			}
209 			case TRAN_SETTING_INT32:
210 				fSettings->SetGetInt32(maps[i].name, &value);
211 				break;
212 		}
213 		fSettings->SaveSettings();
214 	}
215 }
216