1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 12:47:45 +00:00

LibGUI: Port the drag&drop code to Core::MimeData

This commit is contained in:
Andreas Kling 2020-02-14 13:18:34 +01:00
parent 3cba9c3c25
commit 814d59f462
10 changed files with 59 additions and 53 deletions

View file

@ -31,6 +31,7 @@
#include <AK/StringBuilder.h>
#include <AK/URL.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/MimeData.h>
#include <LibCore/UserInfo.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
@ -575,10 +576,10 @@ int main(int argc, char** argv)
directory_view->on_drop = [&](const GUI::AbstractView&, const GUI::ModelIndex& index, const GUI::DropEvent& event) {
if (!index.is_valid())
return;
if (event.data_type() != "url-list")
if (!event.mime_data().has_urls())
return;
auto paths_to_copy = event.data().split('\n');
if (paths_to_copy.is_empty()) {
auto urls = event.mime_data().urls();
if (urls.is_empty()) {
dbg() << "No files to drop";
return;
}
@ -587,8 +588,7 @@ int main(int argc, char** argv)
if (!target_node.is_directory())
return;
for (auto& path_to_copy : paths_to_copy) {
auto url_to_copy = URL(path_to_copy);
for (auto& url_to_copy : urls) {
if (!url_to_copy.is_valid())
continue;
auto new_path = String::format("%s/%s",
@ -597,7 +597,7 @@ int main(int argc, char** argv)
if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
auto error_message = String::format("Could not copy %s into %s.",
path_to_copy.characters(),
url_to_copy.to_string().characters(),
new_path.characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
} else {

View file

@ -26,10 +26,11 @@
#include "QSWidget.h"
#include <AK/URL.h>
#include <LibGfx/Bitmap.h>
#include <LibCore/MimeData.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
QSWidget::QSWidget(GUI::Widget* parent)
: GUI::Frame(parent)
@ -130,15 +131,15 @@ void QSWidget::drop_event(GUI::DropEvent& event)
event.accept();
window()->move_to_front();
if (event.data_type() == "url-list") {
auto lines = event.data().split_view('\n');
if (lines.is_empty())
if (event.mime_data().has_urls()) {
auto urls = event.mime_data().urls();
if (urls.is_empty())
return;
if (lines.size() > 1) {
if (urls.size() > 1) {
GUI::MessageBox::show("QuickShow can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
return;
}
URL url(lines[0]);
auto url = urls.first();
auto bitmap = Gfx::Bitmap::load_from_file(url.path());
if (!bitmap) {
GUI::MessageBox::show(String::format("Failed to open %s", url.to_string().characters()), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());

View file

@ -29,6 +29,7 @@
#include <AK/StringBuilder.h>
#include <AK/URL.h>
#include <LibCore/File.h>
#include <LibCore/MimeData.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
@ -191,12 +192,11 @@ TextEditorWidget::TextEditorWidget()
if (needle.is_empty())
return;
auto found_range = m_editor->document().find_next(needle);
while(found_range.is_valid()) {
m_editor->set_selection(found_range);
m_editor->insert_at_cursor_or_replace_selection(substitute);
found_range = m_editor->document().find_next(needle);
while (found_range.is_valid()) {
m_editor->set_selection(found_range);
m_editor->insert_at_cursor_or_replace_selection(substitute);
found_range = m_editor->document().find_next(needle);
}
});
@ -463,15 +463,14 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event)
event.accept();
window()->move_to_front();
if (event.data_type() == "url-list") {
auto lines = event.data().split_view('\n');
if (lines.is_empty())
if (event.mime_data().has_urls()) {
auto urls = event.mime_data().urls();
if (urls.is_empty() )
return;
if (lines.size() > 1) {
if (urls.size() > 1) {
GUI::MessageBox::show("TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
return;
}
URL url(lines[0]);
open_sesame(url.path());
open_sesame(urls.first().path());
}
}