拖拽类型

拖拽文本EDIT

拖拽文本的时候,使用 text/plain 类型。数据应该是被拖拽的字符串。例如:

event.dataTransfer.setData("text/plain", "This is text to drag")

拖拽网页上的文本框内文本及已选文本是自动完成的,所以你不需要自己处理。

建议你总是添加text/plain类型数据作为不支持其它类型的应用或投放目标的降级,除非没有可选的符合逻辑的文本。总是将纯文本类型添加在最后,因为这是最不具体的数据( as it is the least specific)。

在旧的代码中,你可能会遇到text/unicodeText类型。这些与text/plain类型是等效的,存储、获取到的都是纯文本数据。

链接需要包含两种类型的数据;第一种是text/uri-list类型的URL,第二种是text/plain类型的URL。两种类型要使用相同的数据,即链接的URL。例如:

var dt = event.dataTransfer;
dt.setData("text/uri-list", "http://www.mozilla.org");
dt.setData("text/plain", "http://www.mozilla.org");

与之前一样,将text/plain类型添加在最后,因为它不如uri类型具体。

注意URL类型是uri-list,“uri”中包含的是“i”,而不是“l”。

拖拽多条链接时,你可以使用换行将每条链接分开。以井号(#)开头的行是注释,不应该认为是合法的URL。你可以使用注释来指明链接的含义,或保存与链接相关的标题。text/plain版本的数据应该包含所有的链接但不应该包含注释。

例如:

http://www.mozilla.org
#A second link
http://www.xulplanet.com

这个text/uri-list数据样例包含两条链接和一条注释。

当得到一条拖放的链接,你需要确保你能处理包含多条链接以及注释的数据。出于便利考虑,特殊类型URL可以用来获得text/uri-list类型数据中的第一条合法的链接(译注:chrome则是得到text/uri-list的完整数据)。你不应该使用 URL 类型来添加数据,这样做只是设置了 text/uri-list 类型的数据。

var url = event.dataTransfer.getData("URL");

你也可以使用Mozilla自定义的text/x-moz-url类型。如果使用了,它需要添加在text/uri-list类型之前。他保存链接的URL,后面跟随链接的标题,之间只用断行隔开。例如:

http://www.mozilla.org
Mozilla
http://www.xulplanet.com
XUL Planet

拖拽HTML与XMLEDIT

HTML内容可以使用text/html类型。这种类型的数据需要是序列化的HTML。例如,使用元素的innerHTML属性值来设置这个类型的值是合适的。

XML内容可以使用text/xml类型,但你要确保数据值是格式良好的XML。

你也可以包含使用text/plain类型表示的HTML或XML的纯文本。该数据应该只包含文本内容而不包含源标签或属性。例如:

var dt = event.dataTransfer;
dt.setData("text/html", "Hello there, <strong>stranger</strong>"); dt.setData("text/plain", "Hello there, stranger");

Dragging FilesEDIT

A local file is dragged using the application/x-moz-file type with a data value that is an nsIFile object. Non-privileged web pages are not able to retrieve or modify data of this type. Because a file is not a string, you must use the mozSetDataAt method to assign the data. Similarly, when retrieving the data, you must use the mozGetDataAt method.

event.dataTransfer.mozSetDataAt("application/x-moz-file", file, 0);

If possible, you may also include the file URL of the file using both the text/uri-list and/or text/plain types. These types should be added last so that the more specific application/x-moz-file type has higher priority.

Multiple files will be received during a drop as mutliple items in the data transfer. See Dragging and Dropping Multiple Items for more details about this.

The following example shows how to create an area for receiving dropped files:

<listbox ondragenter="return checkDrag(event)" ondragover="return checkDrag(event)" ondrop="doDrop(event)"/> <script> function checkDrag(event) { return event.dataTransfer.types.contains("application/x-moz-file"); } function doDrop(event) { var file = event.dataTransfer.mozGetDataAt("application/x-moz-file", 0); if (file instanceof Components.interfaces.nsIFile) event.currentTarget.appendItem(file.leafName); } </script>

In this example, the event returns false only if the data transfer contains the application/x-moz-file type. During the drop event, the data associated with the file type is retrieved, and the filename of the file is added to the listbox. Note that the instanceof operator is used here as themozGetDataAt method will return an nsISupports that needs to be checked and converted into an nsIFile. This is also a good extra check in case someone made a mistake and added a non-file for this type.

Dragging ImagesEDIT

Direct image dragging is not commonly done. In fact, Mozilla does not support direct image dragging on Mac or Linux platforms. Instead, images are usually dragged only by their URLs. To do this, use the text/uri-list type as with other URL links. The data should be the URL of the image or a data URL if the image is not stored on a web site or disk. For more information about data URLs, see the data URL scheme.

As with other links, the data for the text/plain type should also contain the URL. However, a data URL is not usually as useful in a text context, so you may wish to exclude the text/plain data in this situation.

In chrome or other privileged code, you may also use the image/jpegimage/png or image/gif types, depending on the type of image. The data should be an object which implements the nsIInputStream interface. When this stream is read, it should provide the data bits for the image, as if the image was a file of that type.

You should also include the application/x-moz-file type if the image is located on disk. In fact, this a common way in which image files are dragged.

It is important to set the data in the right order, from most specific to least specific. The image type such as image/jpeg should come first, followed by the application/x-moz-file type. Next, you should set the text/uri-list data and finally the text/plain data. For example:

var dt = event.dataTransfer;
dt.mozSetDataAt("image/png", stream, 0);
dt.mozSetDataAt("application/x-moz-file", file, 0);
dt.setData("text/uri-list", imageurl);
dt.setData("text/plain", imageurl);

Note that the mozGetDataAt method is used for non-text data. As some contexts may only include some of these types, it is important to check which type is made available when receiving dropped images.

Dragging NodesEDIT

Nodes and elements in a document may be dragged using the application/x-moz-node type. This data for the type should be a DOM node. This allows the drop target to receive the actual node where the drag was started from. Note that callers from a different domain will not be able to access the node even when it has been dropped.

You should always include a plain text alternative for the node.

Dragging Custom DataEDIT

You can also use other types that you make up for custom purposes. You should strive to always include a plain text alternative unless that object being dragged is specific to a particular site or application. In this case, the custom type ensures that the data cannot be dropped elsewhere.

Dragging files to an operating system folderEDIT

There are cases in which you may want to add a file to an existing drag event session, and you may also want to write the file to disk when the drop operation happens over a folder in the operating system when your code receives notification of the target folder's location. This only works in extensions (or other privileged code) and the data flavor "application/moz-file-promise" should be used. The following sample offers an overview of this advanced case:

// currentEvent is a given existing drag operation event

currentEvent.dataTransfer.setData("text/x-moz-url", URL);
currentEvent.dataTransfer.setData("application/x-moz-file-promise-url", URL);
currentEvent.dataTransfer.setData("application/x-moz-file-promise-filename", leafName);
currentEvent.dataTransfer.mozSetDataAt('application/x-moz-file-promise',
                  new dataProvider(success,error),
                  0, Components.interfaces.nsISupports);

function dataProvider(){} 

dataProvider.prototype = {
  QueryInterface : function(iid) {
    if (iid.equals(Components.interfaces.nsIFlavorDataProvider)
                  || iid.equals(Components.interfaces.nsISupports))
      return this;
    throw Components.results.NS_NOINTERFACE;
  },
  getFlavorData : function(aTransferable, aFlavor, aData, aDataLen) {
    if (aFlavor == 'application/x-moz-file-promise') {
  
       var urlPrimitive = {};
       var dataSize = {};
  
       aTransferable.getTransferData('application/x-moz-file-promise-url', urlPrimitive, dataSize);
       var url = new String(urlPrimitive.value.QueryInterface(Components.interfaces.nsISupportsString));
       console.log("URL file orignal is = " + url);
      
       var namePrimitive = {};
       aTransferable.getTransferData('application/x-moz-file-promise-filename', namePrimitive, dataSize);
       var name = new String(namePrimitive.value.QueryInterface(Components.interfaces.nsISupportsString));
  
       console.log("target filename is = " + name);
  
       var dirPrimitive = {};
       aTransferable.getTransferData('application/x-moz-file-promise-dir', dirPrimitive, dataSize);
       var dir = dirPrimitive.value.QueryInterface(Components.interfaces.nsILocalFile);
  
       console.log("target folder is = " + dir.path);
  
       var file = Cc['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
       file.initWithPath(dir.path);
       file.appendRelativePath(name);
  
       console.log("output final path is =" + file.path);
  
       // now you can write or copy the file yourself...
    } 
  }
}

  



来源:https://developer.mozilla.org

转载于:https://www.cnblogs.com/yuzhongwusan/p/5457538.html

### 回答1: Qt是一个跨平台的应用程序开发框架,提供了丰富的功能和灵活的界面设计方式,方便开发人员快速构建各种应用程序。而“类型拖拽停靠”则是Qt中的一种界面设计模式,用于实现窗口之间的交互和布局管理。 在Qt中,开发人员可以使用类型拖拽停靠模式,实现窗口的拖拽、停靠和分离功能。通过该模式,用户可以方便地重新排列窗口、调整布局,并快速切换不同的视图。这种模式下,窗口之间可以通过拖拽的方式进行移动,以便更好地适应用户的工作需求。 类型拖拽停靠模式在Qt中使用了一些特定的类和方法来实现,如QDockWidget类用于创建可停靠的窗口,可以将其插入到主窗口的任何边缘位置,也可以浮动在主窗口的任意位置。另外,还可以通过使用QMdiArea类来管理多个子窗口,允许用户以平铺或层叠的方式查看和切换窗口。 此外,在类型拖拽停靠模式下,还可以根据需要创建自定义的停靠窗口,通过实现自定义的QDockWidget子类,并重写其中的方法,实现特定的行为和功能。 总之,Qt中的类型拖拽停靠模式提供了一种灵活而强大的界面设计方式,使得开发人员可以轻松地实现窗口间的交互、布局管理和调整,为用户提供更好的使用体验。同时,Qt作为一款跨平台的开发框架,不仅支持类型拖拽停靠等丰富的界面设计功能,还提供了丰富的其他功能和工具,方便开发人员快速构建各种应用程序。 ### 回答2: Qt和类型拖拽停靠(Docking)是一种常见的用户界面设计技术,用于创建具有可自定义布局和组织功能的应用程序界面。 Qt是一个跨平台的C++应用程序框架,提供了丰富的图形界面工具和库。它具有良好的可扩展性和兼容性,能够在不同的操作系统上运行,并且支持多种开发语言。Qt使用信号与槽机制来实现组件间的通信,使得编写界面逻辑变得简单且高效。 类型拖拽停靠是一种让用户通过拖拽和停靠的方式对界面组件进行排列和布局的技术。用户可以将程序的窗口和其他界面元素拖拽到特定的停靠区域,从而改变它们之间的布局关系。这种技术使得用户可以根据自己的需求自由地组织和管理应用程序的界面,提高了用户的工作效率和舒适度。 Qt提供了一些内置的类和接口,用于实现拖拽和停靠功能。例如,QDockWidget类可以将一个窗口部件添加到一个可停靠的区域,并提供了一些方法来实现拖拽和停靠的行为。另外,Qt还提供了一些布局管理器,如QGridLayout和QVBoxLayout,可以帮助开发者实现更复杂的界面布局。 总结来说,Qt和类型拖拽停靠是一种强大的界面设计技术,可以让开发者和用户自由地组织和管理应用程序的界面。它们结合了Qt框架的可拓展性和跨平台性,为开发者提供了一种简单而高效的方式来创建优秀的用户界面。 ### 回答3: Qt的类型拖拽停靠是指在Qt框架中进行窗口布局和交互的一种方式。它能够让用户通过鼠标操作来拖拽和停靠不同类型的窗口。 拖拽停靠的一个重要应用场景是在IDE(集成开发环境)中,比如Qt Creator。在这样的环境下,用户可以通过拖拽和停靠不同类型的面板,如源代码编辑器、项目管理器和调试器等,来定制自己的工作区布局,以适应个人的需求。 类型拖拽停靠的实现通常通过使用Qt框架中的QDockWidget类和QMainWindow类来完成。QDockWidget可以被拖拽和停靠到主窗口中,而QMainWindow作为主窗口提供容纳和管理这些QDockWidget的功能。 在实际使用中,用户可以通过拖拽面板的标题栏或边缘,将面板从一个位置拖拽到另一个位置,并通过停靠的方式将其固定在某个位置。拖拽停靠还支持分隔条,允许用户调整不同面板之间的大小比例。 这种方式的好处是用户可以根据自己的习惯和需求来自定义界面布局,提高工作效率。同时,还可以根据具体的任务,根据需要打开或关闭不同类型的面板,以便更好地专注于当前的工作。 总之,Qt的类型拖拽停靠是一种方便灵活的窗口布局和交互方式,可以提供个性化的工作环境,并为用户提供更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值