Example #1
0
	def requestFinished(self, request_id, error_flag) :
		if request_id != self.__http_request_id :
			return

		if error_flag and not self.__http_abort_flag :
			Qt.QMessageBox.warning(None, Const.MyName, tr("HTTP error: %1\nPress \"Yes\" to ignore").arg(self.__http.errorString()))

		self.__timer.stop()

		###

		text = Qt.QTextCodec.codecForName("UTF-8").toUnicode(self.__http_output.data())

		try :
			raw_dict = json.loads(unicode(text).encode("utf-8"))
			if raw_dict.has_key("sentences") :
				lang_codes_dict = LangsList.langCodes()
				sl_name = LangsList.langName(raw_dict.get("src", self.__sl), lang_codes_dict)
				tl_name = LangsList.langName(self.__tl, lang_codes_dict)

				text = reduce(lambda a, b : a + b, map(lambda arg : arg["trans"], raw_dict["sentences"]))
				text = tr("<font class=\"word_header_font\">Translated: %1 &#187; %2</font><hr>%3").arg(sl_name).arg(tl_name).arg(text)
			else :
				text = ( tr("<font class=\"word_header_font\">Invalid server response</font><hr>Raw JSON: %1")
					.arg(Qt.QString(unicode(text).encode("utf-8"))) )
		except :
			text = ( tr("<font class=\"word_header_font\">Invalid server response</font><hr>Raw JSON: %1")
				.arg(Qt.QString(unicode(text).encode("utf-8"))) )
			Logger.warning(text)
			Logger.warning("JSON parser exception")
			Logger.attachException(Logger.WarningMessage)

		self.textChangedSignal(text)
		self.processFinishedSignal()
Example #2
0
	def loadInfo(self, dict_name) :
		dict_name = str(dict_name)

		dict_file_name = Utils.joinPath(Const.AllDictsDirPath, dict_name)
		dict_file = Qt.QFile(dict_file_name)
		dict_file_stream = Qt.QTextStream(dict_file)
		if not dict_file.open(Qt.QIODevice.ReadOnly) :
			Logger.warning(Qt.QString("Cannot open dict file \"%1\" for reading info").arg(dict_file_name))
			return

		self.__info_cache_dict[dict_name] = {}
		for all_tags_list_item in AllTagsList :
			self.__info_cache_dict[dict_name][all_tags_list_item] = Qt.QString()

		while not dict_file_stream.atEnd() :
			Qt.QCoreApplication.processEvents(Qt.QEventLoop.ExcludeUserInputEvents)
			line = dict_file_stream.readLine()

			if line.isEmpty() :
				continue
			if line[0] != "#" and line.contains("  ") :
				break

			if line[0] == "#" :
				line.remove(0, 1)
				line = line.trimmed()

				key = MiscTag
				for key_item in self.__info_cache_dict[dict_name].keys() :
					tag = Qt.QString(key_item+":")
					if line.startsWith(tag) :
						line = line.remove(0, tag.length()).simplified()
						key = str(key_item)
						break
				if not self.__info_cache_dict[dict_name][key].isEmpty() :
					self.__info_cache_dict[dict_name][key].append("<br>")
				self.__info_cache_dict[dict_name][key].append(line)

		dict_file.close()

		###

		self.__info_cache_dict[dict_name][FileSizeTag] = Qt.QString().setNum(dict_file.size() / 1024)

		lang_codes_dict = LangsList.langCodes()
		direction_regexp = Qt.QRegExp("((..)-(..))")
		if direction_regexp.exactMatch(self.__info_cache_dict[dict_name][DirectionTag]) :
			icon_width = icon_height = Qt.QApplication.style().pixelMetric(Qt.QStyle.PM_SmallIconSize)
			self.__info_cache_dict[dict_name][DirectionTag] = (
				Qt.QString("<img src=\"%3\" width=\"%1\" height=\"%2\"> &#187; <img src=\"%4\" width=\"%1\" height=\"%2\">"
					"&nbsp;&nbsp;&nbsp;%5 &#187; %6 (%7)").arg(icon_width).arg(icon_height)
						.arg(IconsLoader.iconPath(Utils.joinPath("flags", direction_regexp.cap(2))))
						.arg(IconsLoader.iconPath(Utils.joinPath("flags", direction_regexp.cap(3))))
						.arg(LangsList.langName(direction_regexp.cap(2), lang_codes_dict))
						.arg(LangsList.langName(direction_regexp.cap(3), lang_codes_dict))
						.arg(direction_regexp.cap(1)) )

		for tag_key in self.__info_cache_dict[dict_name].keys() :
			if self.__info_cache_dict[dict_name][tag_key].isEmpty() :
				self.__info_cache_dict[dict_name][tag_key] = tr("Unavailable")
	def translateUi(self) :
		self.setWindowTitle(tr("Google Translate"))
		self.__translate_button.setText(tr("T&ranslate"))
		self.__translate_button.setToolTip(tr("Ctrl+Enter"))

		###

		lang_codes_dict = LangsList.langCodes()
		langs_list = LangsList.langs()
		main_lang = Locale.Locale().mainLang()

		sl_lang = self.__sl_combobox.itemData(self.__sl_combobox.currentIndex()).toString()
		self.__sl_combobox.clear()
		self.__sl_combobox.addItem(IconsLoader.icon("help-hint"), tr("Guess"), Qt.QVariant(""))
		self.__sl_combobox.insertSeparator(1)

		tl_lang = self.__tl_combobox.itemData(self.__tl_combobox.currentIndex()).toString()
		self.__tl_combobox.clear()
		self.__tl_combobox.addItem(IconsLoader.icon(Utils.joinPath("flags", main_lang)),
			LangsList.langName(main_lang, lang_codes_dict), Qt.QVariant(main_lang))
		self.__tl_combobox.insertSeparator(1)

		for combobox in (self.__sl_combobox, self.__tl_combobox) :
			for langs_list_item in langs_list :
				combobox.addItem(IconsLoader.icon(Utils.joinPath("flags", langs_list_item["code"])),
					langs_list_item["name"], Qt.QVariant(langs_list_item["code"]))

		for (combobox, lang) in ((self.__sl_combobox, sl_lang), (self.__tl_combobox, tl_lang)) :
			for count in xrange(combobox.count()) :
				if combobox.itemData(count).toString() == lang and not combobox.itemText(count).isEmpty() :
					combobox.setCurrentIndex(count)
					break
Example #4
0
	def setCaptions(self) :
		dict_name_regexp = Qt.QRegExp("([^\\.]+)\\.((..)-(..))")
		if dict_name_regexp.exactMatch(self.__dict_name) :
			lang_codes_dict = LangsList.langCodes()

			dict_caption = dict_name_regexp.cap(1).replace(Qt.QRegExp("[\\._]"), " ")
			dict_direction = dict_name_regexp.cap(2)
			sl_lang_name = LangsList.langName(dict_name_regexp.cap(3), lang_codes_dict)
			tl_lang_name = LangsList.langName(dict_name_regexp.cap(4), lang_codes_dict)

			self.__dict_caption_label.setText(Utils.styledHtml(self.__css.css(),
				Qt.QString("<font class=\"text_label_font\">%1</font>").arg(dict_caption)))
			self.__dict_direction_label.setText(Utils.styledHtml(self.__css.css(),
				 Qt.QString("<font class=\"text_label_font\">%1</font>").arg(dict_direction)))
			self.__dict_full_direction_label.setText(Qt.QString("%1 &#187; %2").arg(sl_lang_name).arg(tl_lang_name))
		else :
			self.__dict_caption_label.setText(Utils.styledHtml(self.__css.css(),
				Qt.QString("<font class=\"text_label_font\">%1</font>").arg(self.__dict_name)))
Example #5
0
	def translateUi(self) :
		self.__show_tray_icon_checkbox.setText(tr("Show tray icon"))
		self.__show_splash_checkbox.setText(tr("Show splash screen on startup"))
		self.__force_main_lang_label.setText(tr("Force language:"))
		self.__debug_mode_checkbox.setText(tr("Debug mode (write info to stderr)"))

		###

		last_index = self.__force_main_lang_combobox.currentIndex()
		self.__force_main_lang_combobox.clear()

		lang_codes_dict = LangsList.langCodes()
		system_lang = Locale.Locale.systemLang()

		self.__force_main_lang_combobox.addItem(IconsLoader.icon(Utils.joinPath("flags", system_lang)),
			tr("By default (%1)").arg(LangsList.langName(system_lang, lang_codes_dict)), Qt.QVariant(""))

		self.__force_main_lang_combobox.insertSeparator(1)

		for langs_list_item in Locale.Locale.validLangs() :
			self.__force_main_lang_combobox.addItem(IconsLoader.icon(Utils.joinPath("flags", langs_list_item)),
				LangsList.langName(langs_list_item, lang_codes_dict), Qt.QVariant(langs_list_item))
		self.__force_main_lang_combobox.setCurrentIndex(last_index)