// Wipe the currently displayed text.
KLineEdit::setText(QLatin1String(""));
- // Create an input method event attribute list.
- QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
+ // Only highlight the URL if it starts with a known schema.
+ if (urlString.startsWith(QLatin1String("https://")) || urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:")))
+ {
+ // Create an input method event attribute list.
+ QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
- // Calculate the URL string length.
- int urlStringLength = urlString.length();
+ // Calculate the URL string length.
+ int urlStringLength = urlString.length();
- // Get the index of end of the schema.
- int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
+ // Get the index of end of the schema.
+ int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
- // Get the index of the last `.` in the domain.
- int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'));
+ // Get the index of the `/` immediately after the domain name. If this doesn't exit it will be set to `-1`.
+ int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
- // Get the index of the penultimate `.` in the domain. If this doesn't exist it will be set to `-1`.
- int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
+ // Get the index of the last `.` in the domain.
+ int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'), endOfDomainNameIndex - urlStringLength);
- // Get the index of the `/` immediately after the domain name. If this doesn't exit it will be set to `-1`.
- int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
+ // Get the index of the penultimate `.` in the domain. If this doesn't exist it will be set to `-1`.
+ int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
- // Create the text character formats.
- QTextCharFormat grayTextCharFormat;
- QTextCharFormat schemaTextCharFormat;
+ // Create the text character formats.
+ QTextCharFormat grayTextCharFormat;
+ QTextCharFormat schemaTextCharFormat;
- // Set the text character format colors.
- grayTextCharFormat.setForeground(Qt::darkGray);
+ // Set the text character format colors.
+ grayTextCharFormat.setForeground(Qt::darkGray);
- // Set the schema text character format color.
- if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
- schemaTextCharFormat.setForeground(Qt::red);
- else
- schemaTextCharFormat = grayTextCharFormat;
+ // Set the schema text character format color.
+ if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
+ schemaTextCharFormat.setForeground(Qt::red);
+ else
+ schemaTextCharFormat = grayTextCharFormat;
- // Append the schema text format format.
- inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
+ // Append the schema text format format.
+ inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
- // Append the subdomain format if the subdomain exists. If it doesn't, the penultimate dot index will be `-1`.
- if (penultimateDotIndex > 0)
- inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
+ // Append the subdomain format if the subdomain exists. If it doesn't, the penultimate dot index will be `-1`.
+ if (penultimateDotIndex > 0)
+ inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
- // Append the post domain formatting if text exists after the domain name. If it doesn't, the end of domain name index will be `-1`.
- if (endOfDomainNameIndex > 0)
- inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
+ // Append the post domain formatting if text exists after the domain name. If it doesn't, the end of domain name index will be `-1`.
+ if (endOfDomainNameIndex > 0)
+ inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
- // Create an input method event with an empty pre-edit string.
- QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
+ // Create an input method event with an empty pre-edit string.
+ QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
- // Apply the URL highlighting.
- event(&inputMethodEvent);
+ // Apply the URL highlighting.
+ event(&inputMethodEvent);
+ }
// Run the default commands.
KLineEdit::setText(urlString);